Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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; |
| 18 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 19 | import com.android.internal.annotations.VisibleForTesting; |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 20 | import com.android.internal.widget.LockPatternUtils; |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 21 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.UserInfo; |
| 25 | import android.database.Cursor; |
| 26 | import android.database.sqlite.SQLiteDatabase; |
| 27 | import android.database.sqlite.SQLiteOpenHelper; |
| 28 | import android.os.Environment; |
| 29 | import android.os.UserManager; |
| 30 | import android.util.ArrayMap; |
| 31 | import android.util.Log; |
| 32 | import android.util.Slog; |
| 33 | |
| 34 | import java.io.File; |
| 35 | import java.io.IOException; |
| 36 | import java.io.RandomAccessFile; |
| 37 | |
| 38 | import static android.content.Context.USER_SERVICE; |
| 39 | |
| 40 | /** |
| 41 | * Storage for the lock settings service. |
| 42 | */ |
| 43 | class LockSettingsStorage { |
| 44 | |
| 45 | private static final String TAG = "LockSettingsStorage"; |
| 46 | private static final String TABLE = "locksettings"; |
| 47 | |
| 48 | private static final String COLUMN_KEY = "name"; |
| 49 | private static final String COLUMN_USERID = "user"; |
| 50 | private static final String COLUMN_VALUE = "value"; |
| 51 | |
| 52 | private static final String[] COLUMNS_FOR_QUERY = { |
| 53 | COLUMN_VALUE |
| 54 | }; |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 55 | private static final String[] COLUMNS_FOR_PREFETCH = { |
| 56 | COLUMN_KEY, COLUMN_VALUE |
| 57 | }; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 58 | |
| 59 | private static final String SYSTEM_DIRECTORY = "/system/"; |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 60 | private static final String LOCK_PATTERN_FILE = "gatekeeper.pattern.key"; |
| 61 | private static final String BASE_ZERO_LOCK_PATTERN_FILE = "gatekeeper.gesture.key"; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 62 | private static final String LEGACY_LOCK_PATTERN_FILE = "gesture.key"; |
| 63 | private static final String LOCK_PASSWORD_FILE = "gatekeeper.password.key"; |
| 64 | private static final String LEGACY_LOCK_PASSWORD_FILE = "password.key"; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 65 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 66 | private static final Object DEFAULT = new Object(); |
| 67 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 68 | private final DatabaseHelper mOpenHelper; |
| 69 | private final Context mContext; |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 70 | private final Cache mCache = new Cache(); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 71 | private final Object mFileWriteLock = new Object(); |
| 72 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 73 | private int mStoredCredentialType; |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 74 | private LockPatternUtils mLockPatternUtils; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 75 | |
| 76 | class CredentialHash { |
| 77 | static final int TYPE_NONE = -1; |
| 78 | static final int TYPE_PATTERN = 1; |
| 79 | static final int TYPE_PASSWORD = 2; |
| 80 | |
| 81 | static final int VERSION_LEGACY = 0; |
| 82 | static final int VERSION_GATEKEEPER = 1; |
| 83 | |
| 84 | CredentialHash(byte[] hash, int version) { |
| 85 | this.hash = hash; |
| 86 | this.version = version; |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 87 | this.isBaseZeroPattern = false; |
| 88 | } |
| 89 | |
| 90 | CredentialHash(byte[] hash, boolean isBaseZeroPattern) { |
| 91 | this.hash = hash; |
| 92 | this.version = VERSION_GATEKEEPER; |
| 93 | this.isBaseZeroPattern = isBaseZeroPattern; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | byte[] hash; |
| 97 | int version; |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 98 | boolean isBaseZeroPattern; |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 101 | public LockSettingsStorage(Context context, Callback callback) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 102 | mContext = context; |
| 103 | mOpenHelper = new DatabaseHelper(context, callback); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 104 | mLockPatternUtils = new LockPatternUtils(context); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 107 | public void writeKeyValue(String key, String value, int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 108 | writeKeyValue(mOpenHelper.getWritableDatabase(), key, value, userId); |
| 109 | } |
| 110 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 111 | public void writeKeyValue(SQLiteDatabase db, String key, String value, int userId) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 112 | ContentValues cv = new ContentValues(); |
| 113 | cv.put(COLUMN_KEY, key); |
| 114 | cv.put(COLUMN_USERID, userId); |
| 115 | cv.put(COLUMN_VALUE, value); |
| 116 | |
| 117 | db.beginTransaction(); |
| 118 | try { |
| 119 | db.delete(TABLE, COLUMN_KEY + "=? AND " + COLUMN_USERID + "=?", |
| 120 | new String[] {key, Integer.toString(userId)}); |
| 121 | db.insert(TABLE, null, cv); |
| 122 | db.setTransactionSuccessful(); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 123 | mCache.putKeyValue(key, value, userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 124 | } finally { |
| 125 | db.endTransaction(); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 130 | public String readKeyValue(String key, String defaultValue, int userId) { |
| 131 | int version; |
| 132 | synchronized (mCache) { |
| 133 | if (mCache.hasKeyValue(key, userId)) { |
| 134 | return mCache.peekKeyValue(key, defaultValue, userId); |
| 135 | } |
| 136 | version = mCache.getVersion(); |
| 137 | } |
| 138 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 139 | Cursor cursor; |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 140 | Object result = DEFAULT; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 141 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 142 | if ((cursor = db.query(TABLE, COLUMNS_FOR_QUERY, |
| 143 | COLUMN_USERID + "=? AND " + COLUMN_KEY + "=?", |
| 144 | new String[] { Integer.toString(userId), key }, |
| 145 | null, null, null)) != null) { |
| 146 | if (cursor.moveToFirst()) { |
| 147 | result = cursor.getString(0); |
| 148 | } |
| 149 | cursor.close(); |
| 150 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 151 | mCache.putKeyValueIfUnchanged(key, result, userId, version); |
| 152 | return result == DEFAULT ? defaultValue : (String) result; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 155 | public void prefetchUser(int userId) { |
| 156 | int version; |
| 157 | synchronized (mCache) { |
| 158 | if (mCache.isFetched(userId)) { |
| 159 | return; |
| 160 | } |
| 161 | mCache.setFetched(userId); |
| 162 | version = mCache.getVersion(); |
| 163 | } |
| 164 | |
| 165 | Cursor cursor; |
| 166 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 167 | if ((cursor = db.query(TABLE, COLUMNS_FOR_PREFETCH, |
| 168 | COLUMN_USERID + "=?", |
| 169 | new String[] { Integer.toString(userId) }, |
| 170 | null, null, null)) != null) { |
| 171 | while (cursor.moveToNext()) { |
| 172 | String key = cursor.getString(0); |
| 173 | String value = cursor.getString(1); |
| 174 | mCache.putKeyValueIfUnchanged(key, value, userId, version); |
| 175 | } |
| 176 | cursor.close(); |
| 177 | } |
| 178 | |
| 179 | // Populate cache by reading the password and pattern files. |
| 180 | readPasswordHash(userId); |
| 181 | readPatternHash(userId); |
| 182 | } |
| 183 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 184 | public int getStoredCredentialType(int userId) { |
| 185 | if (mStoredCredentialType != 0) { |
| 186 | return mStoredCredentialType; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 187 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 188 | |
| 189 | CredentialHash pattern = readPatternHash(userId); |
| 190 | if (pattern == null) { |
| 191 | if (readPasswordHash(userId) != null) { |
| 192 | mStoredCredentialType = CredentialHash.TYPE_PASSWORD; |
| 193 | } else { |
| 194 | mStoredCredentialType = CredentialHash.TYPE_NONE; |
| 195 | } |
| 196 | } else { |
| 197 | CredentialHash password = readPasswordHash(userId); |
| 198 | if (password != null) { |
| 199 | // Both will never be GateKeeper |
| 200 | if (password.version == CredentialHash.VERSION_GATEKEEPER) { |
| 201 | mStoredCredentialType = CredentialHash.TYPE_PASSWORD; |
| 202 | } else { |
| 203 | mStoredCredentialType = CredentialHash.TYPE_PATTERN; |
| 204 | } |
| 205 | } else { |
| 206 | mStoredCredentialType = CredentialHash.TYPE_PATTERN; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return mStoredCredentialType; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | public CredentialHash readPasswordHash(int userId) { |
| 215 | byte[] stored = readFile(getLockPasswordFilename(userId)); |
| 216 | if (stored != null && stored.length > 0) { |
| 217 | return new CredentialHash(stored, CredentialHash.VERSION_GATEKEEPER); |
| 218 | } |
| 219 | |
| 220 | stored = readFile(getLegacyLockPasswordFilename(userId)); |
| 221 | if (stored != null && stored.length > 0) { |
| 222 | return new CredentialHash(stored, CredentialHash.VERSION_LEGACY); |
| 223 | } |
| 224 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 225 | return null; |
| 226 | } |
| 227 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 228 | public CredentialHash readPatternHash(int userId) { |
| 229 | byte[] stored = readFile(getLockPatternFilename(userId)); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 230 | if (stored != null && stored.length > 0) { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 231 | return new CredentialHash(stored, CredentialHash.VERSION_GATEKEEPER); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 232 | } |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 233 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 234 | stored = readFile(getBaseZeroLockPatternFilename(userId)); |
| 235 | if (stored != null && stored.length > 0) { |
| 236 | return new CredentialHash(stored, true); |
| 237 | } |
| 238 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 239 | stored = readFile(getLegacyLockPatternFilename(userId)); |
| 240 | if (stored != null && stored.length > 0) { |
| 241 | return new CredentialHash(stored, CredentialHash.VERSION_LEGACY); |
| 242 | } |
| 243 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 244 | return null; |
| 245 | } |
| 246 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 247 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 248 | public boolean hasPassword(int userId) { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 249 | return hasFile(getLockPasswordFilename(userId)) || |
| 250 | hasFile(getLegacyLockPasswordFilename(userId)); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 253 | public boolean hasPattern(int userId) { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 254 | return hasFile(getLockPatternFilename(userId)) || |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 255 | hasFile(getBaseZeroLockPatternFilename(userId)) || |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 256 | hasFile(getLegacyLockPatternFilename(userId)); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 259 | private boolean hasFile(String name) { |
| 260 | byte[] contents = readFile(name); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 261 | return contents != null && contents.length > 0; |
| 262 | } |
| 263 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 264 | private byte[] readFile(String name) { |
| 265 | int version; |
| 266 | synchronized (mCache) { |
| 267 | if (mCache.hasFile(name)) { |
| 268 | return mCache.peekFile(name); |
| 269 | } |
| 270 | version = mCache.getVersion(); |
| 271 | } |
| 272 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 273 | RandomAccessFile raf = null; |
| 274 | byte[] stored = null; |
| 275 | try { |
| 276 | raf = new RandomAccessFile(name, "r"); |
| 277 | stored = new byte[(int) raf.length()]; |
| 278 | raf.readFully(stored, 0, stored.length); |
| 279 | raf.close(); |
| 280 | } catch (IOException e) { |
| 281 | Slog.e(TAG, "Cannot read file " + e); |
| 282 | } finally { |
| 283 | if (raf != null) { |
| 284 | try { |
| 285 | raf.close(); |
| 286 | } catch (IOException e) { |
| 287 | Slog.e(TAG, "Error closing file " + e); |
| 288 | } |
| 289 | } |
| 290 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 291 | mCache.putFileIfUnchanged(name, stored, version); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 292 | return stored; |
| 293 | } |
| 294 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 295 | private void writeFile(String name, byte[] hash) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 296 | synchronized (mFileWriteLock) { |
| 297 | RandomAccessFile raf = null; |
| 298 | try { |
| 299 | // Write the hash to file |
| 300 | raf = new RandomAccessFile(name, "rw"); |
| 301 | // Truncate the file if pattern is null, to clear the lock |
| 302 | if (hash == null || hash.length == 0) { |
| 303 | raf.setLength(0); |
| 304 | } else { |
| 305 | raf.write(hash, 0, hash.length); |
| 306 | } |
| 307 | raf.close(); |
| 308 | } catch (IOException e) { |
| 309 | Slog.e(TAG, "Error writing to file " + e); |
| 310 | } finally { |
| 311 | if (raf != null) { |
| 312 | try { |
| 313 | raf.close(); |
| 314 | } catch (IOException e) { |
| 315 | Slog.e(TAG, "Error closing file " + e); |
| 316 | } |
| 317 | } |
| 318 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 319 | mCache.putFile(name, hash); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 323 | private void deleteFile(String name) { |
| 324 | File f = new File(name); |
| 325 | if (f != null) { |
| 326 | f.delete(); |
| 327 | } |
| 328 | } |
| 329 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 330 | public void writePatternHash(byte[] hash, int userId) { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 331 | mStoredCredentialType = hash == null |
| 332 | ? CredentialHash.TYPE_NONE |
| 333 | : CredentialHash.TYPE_PATTERN; |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 334 | writeFile(getLockPatternFilename(userId), hash); |
Robin Lee | 68e4ba4 | 2015-03-10 12:34:28 +0000 | [diff] [blame] | 335 | clearPasswordHash(userId); |
| 336 | } |
| 337 | |
| 338 | private void clearPatternHash(int userId) { |
| 339 | writeFile(getLockPatternFilename(userId), null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | public void writePasswordHash(byte[] hash, int userId) { |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 343 | mStoredCredentialType = hash == null |
| 344 | ? CredentialHash.TYPE_NONE |
| 345 | : CredentialHash.TYPE_PASSWORD; |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 346 | writeFile(getLockPasswordFilename(userId), hash); |
Robin Lee | 68e4ba4 | 2015-03-10 12:34:28 +0000 | [diff] [blame] | 347 | clearPatternHash(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Robin Lee | 68e4ba4 | 2015-03-10 12:34:28 +0000 | [diff] [blame] | 350 | private void clearPasswordHash(int userId) { |
| 351 | writeFile(getLockPasswordFilename(userId), null); |
| 352 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 353 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 354 | @VisibleForTesting |
| 355 | String getLockPatternFilename(int userId) { |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 356 | return getLockCredentialFilePathForUser(userId, LOCK_PATTERN_FILE); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 359 | @VisibleForTesting |
| 360 | String getLockPasswordFilename(int userId) { |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 361 | return getLockCredentialFilePathForUser(userId, LOCK_PASSWORD_FILE); |
| 362 | } |
| 363 | |
Andres Morales | 8fa5665 | 2015-03-31 09:19:50 -0700 | [diff] [blame] | 364 | @VisibleForTesting |
| 365 | String getLegacyLockPatternFilename(int userId) { |
| 366 | return getLockCredentialFilePathForUser(userId, LEGACY_LOCK_PATTERN_FILE); |
| 367 | } |
| 368 | |
| 369 | @VisibleForTesting |
| 370 | String getLegacyLockPasswordFilename(int userId) { |
| 371 | return getLockCredentialFilePathForUser(userId, LEGACY_LOCK_PASSWORD_FILE); |
| 372 | } |
| 373 | |
Andres Morales | e40bad8 | 2015-05-28 14:21:36 -0700 | [diff] [blame] | 374 | private String getBaseZeroLockPatternFilename(int userId) { |
| 375 | return getLockCredentialFilePathForUser(userId, BASE_ZERO_LOCK_PATTERN_FILE); |
| 376 | } |
| 377 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 378 | private String getLockCredentialFilePathForUser(int userId, String basename) { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 379 | userId = getUserParentOrSelfId(userId); |
| 380 | String dataSystemDirectory = |
| 381 | android.os.Environment.getDataDirectory().getAbsolutePath() + |
| 382 | SYSTEM_DIRECTORY; |
| 383 | if (userId == 0) { |
| 384 | // Leave it in the same place for user 0 |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 385 | return dataSystemDirectory + basename; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 386 | } else { |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 387 | return new File(Environment.getUserSystemDirectory(userId), basename).getAbsolutePath(); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | private int getUserParentOrSelfId(int userId) { |
Clara Bayarri | 965da39 | 2015-10-28 17:53:53 +0000 | [diff] [blame] | 392 | // Device supports per user encryption, so lock is applied to the given user. |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 393 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
Clara Bayarri | 07b668e | 2015-10-13 12:28:26 +0100 | [diff] [blame] | 394 | return userId; |
| 395 | } |
| 396 | // Device uses Block Based Encryption, and the parent user's lock is used for the whole |
| 397 | // device. |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 398 | if (userId != 0) { |
| 399 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 400 | final UserInfo pi = um.getProfileParent(userId); |
| 401 | if (pi != null) { |
| 402 | return pi.id; |
| 403 | } |
| 404 | } |
| 405 | return userId; |
| 406 | } |
| 407 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 408 | public void removeUser(int userId) { |
| 409 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 410 | |
| 411 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 412 | final UserInfo parentInfo = um.getProfileParent(userId); |
| 413 | |
Robin Lee | 68e4ba4 | 2015-03-10 12:34:28 +0000 | [diff] [blame] | 414 | if (parentInfo == null) { |
| 415 | // This user owns its lock settings files - safe to delete them |
| 416 | synchronized (mFileWriteLock) { |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 417 | String name = getLockPasswordFilename(userId); |
| 418 | File file = new File(name); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 419 | if (file.exists()) { |
| 420 | file.delete(); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 421 | mCache.putFile(name, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 422 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 423 | name = getLockPatternFilename(userId); |
| 424 | file = new File(name); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 425 | if (file.exists()) { |
| 426 | file.delete(); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 427 | mCache.putFile(name, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | try { |
| 433 | db.beginTransaction(); |
| 434 | db.delete(TABLE, COLUMN_USERID + "='" + userId + "'", null); |
| 435 | db.setTransactionSuccessful(); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 436 | mCache.removeUser(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 437 | } finally { |
| 438 | db.endTransaction(); |
| 439 | } |
| 440 | } |
| 441 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 442 | @VisibleForTesting |
| 443 | void closeDatabase() { |
| 444 | mOpenHelper.close(); |
| 445 | } |
| 446 | |
| 447 | @VisibleForTesting |
| 448 | void clearCache() { |
| 449 | mCache.clear(); |
| 450 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 451 | |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 452 | public interface Callback { |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 453 | void initialize(SQLiteDatabase db); |
| 454 | } |
| 455 | |
| 456 | class DatabaseHelper extends SQLiteOpenHelper { |
| 457 | private static final String TAG = "LockSettingsDB"; |
| 458 | private static final String DATABASE_NAME = "locksettings.db"; |
| 459 | |
| 460 | private static final int DATABASE_VERSION = 2; |
| 461 | |
| 462 | private final Callback mCallback; |
| 463 | |
| 464 | public DatabaseHelper(Context context, Callback callback) { |
| 465 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 466 | setWriteAheadLoggingEnabled(true); |
| 467 | mCallback = callback; |
| 468 | } |
| 469 | |
| 470 | private void createTable(SQLiteDatabase db) { |
| 471 | db.execSQL("CREATE TABLE " + TABLE + " (" + |
| 472 | "_id INTEGER PRIMARY KEY AUTOINCREMENT," + |
| 473 | COLUMN_KEY + " TEXT," + |
| 474 | COLUMN_USERID + " INTEGER," + |
| 475 | COLUMN_VALUE + " TEXT" + |
| 476 | ");"); |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void onCreate(SQLiteDatabase db) { |
| 481 | createTable(db); |
| 482 | mCallback.initialize(db); |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) { |
| 487 | int upgradeVersion = oldVersion; |
| 488 | if (upgradeVersion == 1) { |
| 489 | // Previously migrated lock screen widget settings. Now defunct. |
| 490 | upgradeVersion = 2; |
| 491 | } |
| 492 | |
| 493 | if (upgradeVersion != DATABASE_VERSION) { |
| 494 | Log.w(TAG, "Failed to upgrade database!"); |
| 495 | } |
| 496 | } |
| 497 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Cache consistency model: |
| 501 | * - Writes to storage write directly to the cache, but this MUST happen within the atomic |
| 502 | * section either provided by the database transaction or mWriteLock, such that writes to the |
| 503 | * cache and writes to the backing storage are guaranteed to occur in the same order |
| 504 | * |
| 505 | * - Reads can populate the cache, but because they are no strong ordering guarantees with |
| 506 | * respect to writes this precaution is taken: |
| 507 | * - The cache is assigned a version number that increases every time the cache is modified. |
| 508 | * Reads from backing storage can only populate the cache if the backing storage |
| 509 | * has not changed since the load operation has begun. |
| 510 | * This guarantees that no read operation can shadow a write to the cache that happens |
| 511 | * after it had begun. |
| 512 | */ |
| 513 | private static class Cache { |
| 514 | private final ArrayMap<CacheKey, Object> mCache = new ArrayMap<>(); |
| 515 | private final CacheKey mCacheKey = new CacheKey(); |
| 516 | private int mVersion = 0; |
| 517 | |
| 518 | String peekKeyValue(String key, String defaultValue, int userId) { |
| 519 | Object cached = peek(CacheKey.TYPE_KEY_VALUE, key, userId); |
| 520 | return cached == DEFAULT ? defaultValue : (String) cached; |
| 521 | } |
| 522 | |
| 523 | boolean hasKeyValue(String key, int userId) { |
| 524 | return contains(CacheKey.TYPE_KEY_VALUE, key, userId); |
| 525 | } |
| 526 | |
| 527 | void putKeyValue(String key, String value, int userId) { |
| 528 | put(CacheKey.TYPE_KEY_VALUE, key, value, userId); |
| 529 | } |
| 530 | |
| 531 | void putKeyValueIfUnchanged(String key, Object value, int userId, int version) { |
| 532 | putIfUnchanged(CacheKey.TYPE_KEY_VALUE, key, value, userId, version); |
| 533 | } |
| 534 | |
| 535 | byte[] peekFile(String fileName) { |
| 536 | return (byte[]) peek(CacheKey.TYPE_FILE, fileName, -1 /* userId */); |
| 537 | } |
| 538 | |
| 539 | boolean hasFile(String fileName) { |
| 540 | return contains(CacheKey.TYPE_FILE, fileName, -1 /* userId */); |
| 541 | } |
| 542 | |
| 543 | void putFile(String key, byte[] value) { |
| 544 | put(CacheKey.TYPE_FILE, key, value, -1 /* userId */); |
| 545 | } |
| 546 | |
| 547 | void putFileIfUnchanged(String key, byte[] value, int version) { |
| 548 | putIfUnchanged(CacheKey.TYPE_FILE, key, value, -1 /* userId */, version); |
| 549 | } |
| 550 | |
| 551 | void setFetched(int userId) { |
| 552 | put(CacheKey.TYPE_FETCHED, "isFetched", "true", userId); |
| 553 | } |
| 554 | |
| 555 | boolean isFetched(int userId) { |
| 556 | return contains(CacheKey.TYPE_FETCHED, "", userId); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | private synchronized void put(int type, String key, Object value, int userId) { |
| 561 | // Create a new CachKey here because it may be saved in the map if the key is absent. |
| 562 | mCache.put(new CacheKey().set(type, key, userId), value); |
| 563 | mVersion++; |
| 564 | } |
| 565 | |
| 566 | private synchronized void putIfUnchanged(int type, String key, Object value, int userId, |
| 567 | int version) { |
| 568 | if (!contains(type, key, userId) && mVersion == version) { |
| 569 | put(type, key, value, userId); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | private synchronized boolean contains(int type, String key, int userId) { |
| 574 | return mCache.containsKey(mCacheKey.set(type, key, userId)); |
| 575 | } |
| 576 | |
| 577 | private synchronized Object peek(int type, String key, int userId) { |
| 578 | return mCache.get(mCacheKey.set(type, key, userId)); |
| 579 | } |
| 580 | |
| 581 | private synchronized int getVersion() { |
| 582 | return mVersion; |
| 583 | } |
| 584 | |
| 585 | synchronized void removeUser(int userId) { |
| 586 | for (int i = mCache.size() - 1; i >= 0; i--) { |
| 587 | if (mCache.keyAt(i).userId == userId) { |
| 588 | mCache.removeAt(i); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // Make sure in-flight loads can't write to cache. |
| 593 | mVersion++; |
| 594 | } |
| 595 | |
Adrian Roos | e542499 | 2014-11-07 21:47:17 +0100 | [diff] [blame] | 596 | synchronized void clear() { |
| 597 | mCache.clear(); |
| 598 | mVersion++; |
| 599 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 600 | |
| 601 | private static final class CacheKey { |
| 602 | static final int TYPE_KEY_VALUE = 0; |
| 603 | static final int TYPE_FILE = 1; |
| 604 | static final int TYPE_FETCHED = 2; |
| 605 | |
| 606 | String key; |
| 607 | int userId; |
| 608 | int type; |
| 609 | |
| 610 | public CacheKey set(int type, String key, int userId) { |
| 611 | this.type = type; |
| 612 | this.key = key; |
| 613 | this.userId = userId; |
| 614 | return this; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public boolean equals(Object obj) { |
| 619 | if (!(obj instanceof CacheKey)) |
| 620 | return false; |
| 621 | CacheKey o = (CacheKey) obj; |
| 622 | return userId == o.userId && type == o.type && key.equals(o.key); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public int hashCode() { |
| 627 | return key.hashCode() ^ userId ^ type; |
| 628 | } |
| 629 | } |
| 630 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 631 | } |