Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 18 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 25 | import android.content.pm.UserInfo; |
| 26 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 27 | import static android.Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 28 | import static android.content.Context.USER_SERVICE; |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 29 | import static android.Manifest.permission.READ_PROFILE; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 30 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 31 | import android.database.sqlite.SQLiteDatabase; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 33 | import android.os.IBinder; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 34 | import android.os.Process; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 35 | import android.os.RemoteException; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 36 | import android.os.storage.IMountService; |
| 37 | import android.os.ServiceManager; |
Amith Yamasani | d1645f8 | 2012-06-12 11:53:26 -0700 | [diff] [blame] | 38 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 40 | import android.os.UserManager; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 41 | import android.provider.Settings; |
| 42 | import android.provider.Settings.Secure; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 43 | import android.provider.Settings.SettingNotFoundException; |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 44 | import android.security.KeyStore; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 45 | import android.text.TextUtils; |
| 46 | import android.util.Slog; |
| 47 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 48 | import com.android.internal.widget.ILockSettings; |
| 49 | import com.android.internal.widget.LockPatternUtils; |
| 50 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 51 | import java.util.ArrayList; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 52 | import java.util.Arrays; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 53 | import java.util.List; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Keeps the lock pattern/password data and related settings for each user. |
| 57 | * Used by LockPatternUtils. Needs to be a service because Settings app also needs |
| 58 | * to be able to save lockscreen information for secondary users. |
| 59 | * @hide |
| 60 | */ |
| 61 | public class LockSettingsService extends ILockSettings.Stub { |
| 62 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 63 | private static final String PERMISSION = ACCESS_KEYGUARD_SECURE_STORAGE; |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 64 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 65 | private static final String TAG = "LockSettingsService"; |
| 66 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 67 | private final Context mContext; |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 68 | |
| 69 | private final LockSettingsStorage mStorage; |
| 70 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 71 | private LockPatternUtils mLockPatternUtils; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 72 | private boolean mFirstCallToVold; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 73 | |
| 74 | public LockSettingsService(Context context) { |
| 75 | mContext = context; |
| 76 | // Open the database |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 77 | |
| 78 | mLockPatternUtils = new LockPatternUtils(context); |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 79 | mFirstCallToVold = true; |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 80 | |
| 81 | IntentFilter filter = new IntentFilter(); |
| 82 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 83 | filter.addAction(Intent.ACTION_USER_STARTING); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 84 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 85 | |
| 86 | mStorage = new LockSettingsStorage(context, new LockSettingsStorage.Callback() { |
| 87 | @Override |
| 88 | public void initialize(SQLiteDatabase db) { |
| 89 | // Get the lockscreen default from a system property, if available |
| 90 | boolean lockScreenDisable = SystemProperties.getBoolean( |
| 91 | "ro.lockscreen.disable.default", false); |
| 92 | if (lockScreenDisable) { |
| 93 | mStorage.writeKeyValue(db, LockPatternUtils.DISABLE_LOCKSCREEN_KEY, "1", 0); |
| 94 | } |
| 95 | } |
| 96 | }); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 99 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 100 | @Override |
| 101 | public void onReceive(Context context, Intent intent) { |
Robin Lee | 1096cf8 | 2014-09-01 16:52:47 +0100 | [diff] [blame] | 102 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 103 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 104 | final int userSysUid = UserHandle.getUid(userHandle, Process.SYSTEM_UID); |
| 105 | final KeyStore ks = KeyStore.getInstance(); |
| 106 | |
| 107 | // Clear up keystore in case anything was left behind by previous users |
| 108 | ks.resetUid(userSysUid); |
| 109 | |
| 110 | // If this user has a parent, sync with its keystore password |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 111 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 112 | final UserInfo parentInfo = um.getProfileParent(userHandle); |
| 113 | if (parentInfo != null) { |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 114 | final int parentSysUid = UserHandle.getUid(parentInfo.id, Process.SYSTEM_UID); |
| 115 | ks.syncUid(parentSysUid, userSysUid); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 116 | } |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 117 | } else if (Intent.ACTION_USER_STARTING.equals(intent.getAction())) { |
| 118 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 119 | mStorage.prefetchUser(userHandle); |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 124 | public void systemReady() { |
| 125 | migrateOldData(); |
Adrian Roos | 3dcae68 | 2014-10-29 14:43:56 +0100 | [diff] [blame] | 126 | mStorage.prefetchUser(UserHandle.USER_OWNER); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void migrateOldData() { |
| 130 | try { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 131 | // These Settings moved before multi-user was enabled, so we only have to do it for the |
| 132 | // root user. |
| 133 | if (getString("migrated", null, 0) == null) { |
| 134 | final ContentResolver cr = mContext.getContentResolver(); |
| 135 | for (String validSetting : VALID_SETTINGS) { |
| 136 | String value = Settings.Secure.getString(cr, validSetting); |
| 137 | if (value != null) { |
| 138 | setString(validSetting, value, 0); |
| 139 | } |
| 140 | } |
| 141 | // No need to move the password / pattern files. They're already in the right place. |
| 142 | setString("migrated", "true", 0); |
| 143 | Slog.i(TAG, "Migrated lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 146 | // These Settings changed after multi-user was enabled, hence need to be moved per user. |
| 147 | if (getString("migrated_user_specific", null, 0) == null) { |
| 148 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 149 | final ContentResolver cr = mContext.getContentResolver(); |
| 150 | List<UserInfo> users = um.getUsers(); |
| 151 | for (int user = 0; user < users.size(); user++) { |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 152 | // Migrate owner info |
| 153 | final int userId = users.get(user).id; |
| 154 | final String OWNER_INFO = Secure.LOCK_SCREEN_OWNER_INFO; |
| 155 | String ownerInfo = Settings.Secure.getStringForUser(cr, OWNER_INFO, userId); |
| 156 | if (ownerInfo != null) { |
| 157 | setString(OWNER_INFO, ownerInfo, userId); |
| 158 | Settings.Secure.putStringForUser(cr, ownerInfo, "", userId); |
| 159 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 160 | |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 161 | // Migrate owner info enabled. Note there was a bug where older platforms only |
| 162 | // stored this value if the checkbox was toggled at least once. The code detects |
| 163 | // this case by handling the exception. |
| 164 | final String OWNER_INFO_ENABLED = Secure.LOCK_SCREEN_OWNER_INFO_ENABLED; |
| 165 | boolean enabled; |
| 166 | try { |
| 167 | int ivalue = Settings.Secure.getIntForUser(cr, OWNER_INFO_ENABLED, userId); |
| 168 | enabled = ivalue != 0; |
| 169 | setLong(OWNER_INFO_ENABLED, enabled ? 1 : 0, userId); |
| 170 | } catch (SettingNotFoundException e) { |
| 171 | // Setting was never stored. Store it if the string is not empty. |
| 172 | if (!TextUtils.isEmpty(ownerInfo)) { |
| 173 | setLong(OWNER_INFO_ENABLED, 1, userId); |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 176 | Settings.Secure.putIntForUser(cr, OWNER_INFO_ENABLED, 0, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 177 | } |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 178 | // No need to move the password / pattern files. They're already in the right place. |
| 179 | setString("migrated_user_specific", "true", 0); |
| 180 | Slog.i(TAG, "Migrated per-user lock settings to new location"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 181 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 182 | } catch (RemoteException re) { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 183 | Slog.e(TAG, "Unable to migrate old data", re); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 187 | private final void checkWritePermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 188 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsWrite"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 191 | private final void checkPasswordReadPermission(int userId) { |
Jim Miller | 505329b | 2013-11-08 13:25:36 -0800 | [diff] [blame] | 192 | mContext.enforceCallingOrSelfPermission(PERMISSION, "LockSettingsRead"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 195 | private final void checkReadPermission(String requestedKey, int userId) { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 196 | final int callingUid = Binder.getCallingUid(); |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 197 | for (int i = 0; i < READ_PROFILE_PROTECTED_SETTINGS.length; i++) { |
| 198 | String key = READ_PROFILE_PROTECTED_SETTINGS[i]; |
| 199 | if (key.equals(requestedKey) && mContext.checkCallingOrSelfPermission(READ_PROFILE) |
| 200 | != PackageManager.PERMISSION_GRANTED) { |
| 201 | throw new SecurityException("uid=" + callingUid |
| 202 | + " needs permission " + READ_PROFILE + " to read " |
| 203 | + requestedKey + " for user " + userId); |
| 204 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void setBoolean(String key, boolean value, int userId) throws RemoteException { |
| 210 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 211 | setStringUnchecked(key, userId, value ? "1" : "0"); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void setLong(String key, long value, int userId) throws RemoteException { |
| 216 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 217 | setStringUnchecked(key, userId, Long.toString(value)); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void setString(String key, String value, int userId) throws RemoteException { |
| 222 | checkWritePermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 223 | setStringUnchecked(key, userId, value); |
| 224 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 225 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 226 | private void setStringUnchecked(String key, int userId, String value) { |
| 227 | mStorage.writeKeyValue(key, value, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean getBoolean(String key, boolean defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 232 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 233 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 234 | String value = mStorage.readKeyValue(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 235 | return TextUtils.isEmpty(value) ? |
| 236 | defaultValue : (value.equals("1") || value.equals("true")); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public long getLong(String key, long defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 241 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 242 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 243 | String value = mStorage.readKeyValue(key, null, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 244 | return TextUtils.isEmpty(value) ? defaultValue : Long.parseLong(value); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public String getString(String key, String defaultValue, int userId) throws RemoteException { |
Jim Miller | 158fe19 | 2013-04-17 15:23:55 -0700 | [diff] [blame] | 249 | checkReadPermission(key, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 250 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 251 | return mStorage.readKeyValue(key, defaultValue, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Adrian Roos | 4f78845 | 2014-05-22 20:45:59 +0200 | [diff] [blame] | 254 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 255 | public boolean havePassword(int userId) throws RemoteException { |
| 256 | // Do we need a permissions check here? |
| 257 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 258 | return mStorage.hasPassword(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public boolean havePattern(int userId) throws RemoteException { |
| 263 | // Do we need a permissions check here? |
| 264 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 265 | return mStorage.hasPattern(userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 268 | private void maybeUpdateKeystore(String password, int userHandle) { |
| 269 | final UserManager um = (UserManager) mContext.getSystemService(USER_SERVICE); |
| 270 | final KeyStore ks = KeyStore.getInstance(); |
| 271 | |
| 272 | final List<UserInfo> profiles = um.getProfiles(userHandle); |
| 273 | boolean shouldReset = TextUtils.isEmpty(password); |
| 274 | |
| 275 | // For historical reasons, don't wipe a non-empty keystore if we have a single user with a |
| 276 | // single profile. |
| 277 | if (userHandle == UserHandle.USER_OWNER && profiles.size() == 1) { |
| 278 | if (!ks.isEmpty()) { |
| 279 | shouldReset = false; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | for (UserInfo pi : profiles) { |
| 284 | final int profileUid = UserHandle.getUid(pi.id, Process.SYSTEM_UID); |
| 285 | if (shouldReset) { |
| 286 | ks.resetUid(profileUid); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 287 | } else { |
Robin Lee | f0246a8 | 2014-08-13 09:50:25 +0100 | [diff] [blame] | 288 | ks.passwordUid(password, profileUid); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 293 | @Override |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 294 | public void setLockPattern(String pattern, int userId) throws RemoteException { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 295 | checkWritePermission(userId); |
| 296 | |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 297 | maybeUpdateKeystore(pattern, userId); |
| 298 | |
| 299 | final byte[] hash = LockPatternUtils.patternToHash( |
| 300 | LockPatternUtils.stringToPattern(pattern)); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 301 | mStorage.writePatternHash(hash, userId); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 305 | public void setLockPassword(String password, int userId) throws RemoteException { |
| 306 | checkWritePermission(userId); |
| 307 | |
| 308 | maybeUpdateKeystore(password, userId); |
| 309 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 310 | mStorage.writePasswordHash(mLockPatternUtils.passwordToHash(password, userId), userId); |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public boolean checkPattern(String pattern, int userId) throws RemoteException { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 315 | checkPasswordReadPermission(userId); |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 316 | byte[] hash = LockPatternUtils.patternToHash(LockPatternUtils.stringToPattern(pattern)); |
| 317 | byte[] storedHash = mStorage.readPatternHash(userId); |
| 318 | |
| 319 | if (storedHash == null) { |
| 320 | return true; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 321 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 322 | |
| 323 | boolean matched = Arrays.equals(hash, storedHash); |
| 324 | if (matched && !TextUtils.isEmpty(pattern)) { |
| 325 | maybeUpdateKeystore(pattern, userId); |
| 326 | } |
| 327 | return matched; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
Jim Miller | de1af08 | 2013-09-11 14:58:26 -0700 | [diff] [blame] | 331 | public boolean checkPassword(String password, int userId) throws RemoteException { |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 332 | checkPasswordReadPermission(userId); |
| 333 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 334 | byte[] hash = mLockPatternUtils.passwordToHash(password, userId); |
| 335 | byte[] storedHash = mStorage.readPasswordHash(userId); |
| 336 | |
| 337 | if (storedHash == null) { |
| 338 | return true; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 339 | } |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 340 | |
| 341 | boolean matched = Arrays.equals(hash, storedHash); |
| 342 | if (matched && !TextUtils.isEmpty(password)) { |
| 343 | maybeUpdateKeystore(password, userId); |
| 344 | } |
| 345 | return matched; |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 349 | public boolean checkVoldPassword(int userId) throws RemoteException { |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 350 | if (!mFirstCallToVold) { |
| 351 | return false; |
| 352 | } |
| 353 | mFirstCallToVold = false; |
| 354 | |
| 355 | checkPasswordReadPermission(userId); |
| 356 | |
| 357 | // There's no guarantee that this will safely connect, but if it fails |
| 358 | // we will simply show the lock screen when we shouldn't, so relatively |
| 359 | // benign. There is an outside chance something nasty would happen if |
| 360 | // this service restarted before vold stales out the password in this |
| 361 | // case. The nastiness is limited to not showing the lock screen when |
| 362 | // we should, within the first minute of decrypting the phone if this |
| 363 | // service can't connect to vold, it restarts, and then the new instance |
| 364 | // does successfully connect. |
| 365 | final IMountService service = getMountService(); |
| 366 | String password = service.getPassword(); |
| 367 | service.clearPassword(); |
| 368 | if (password == null) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | try { |
| 373 | if (mLockPatternUtils.isLockPatternEnabled()) { |
| 374 | if (checkPattern(password, userId)) { |
| 375 | return true; |
| 376 | } |
| 377 | } |
| 378 | } catch (Exception e) { |
| 379 | } |
| 380 | |
| 381 | try { |
| 382 | if (mLockPatternUtils.isLockPasswordEnabled()) { |
| 383 | if (checkPassword(password, userId)) { |
| 384 | return true; |
| 385 | } |
| 386 | } |
| 387 | } catch (Exception e) { |
| 388 | } |
| 389 | |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | @Override |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 394 | public void removeUser(int userId) { |
| 395 | checkWritePermission(userId); |
| 396 | |
Adrian Roos | 261d5ab | 2014-10-29 14:42:38 +0100 | [diff] [blame] | 397 | mStorage.removeUser(userId); |
Robin Lee | 49d810c | 2014-09-23 13:50:22 +0100 | [diff] [blame] | 398 | |
| 399 | final KeyStore ks = KeyStore.getInstance(); |
| 400 | final int userUid = UserHandle.getUid(userId, Process.SYSTEM_UID); |
| 401 | ks.resetUid(userUid); |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 404 | private static final String[] VALID_SETTINGS = new String[] { |
| 405 | LockPatternUtils.LOCKOUT_PERMANENT_KEY, |
| 406 | LockPatternUtils.LOCKOUT_ATTEMPT_DEADLINE, |
| 407 | LockPatternUtils.PATTERN_EVER_CHOSEN_KEY, |
| 408 | LockPatternUtils.PASSWORD_TYPE_KEY, |
| 409 | LockPatternUtils.PASSWORD_TYPE_ALTERNATE_KEY, |
| 410 | LockPatternUtils.LOCK_PASSWORD_SALT_KEY, |
| 411 | LockPatternUtils.DISABLE_LOCKSCREEN_KEY, |
| 412 | LockPatternUtils.LOCKSCREEN_OPTIONS, |
| 413 | LockPatternUtils.LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK, |
| 414 | LockPatternUtils.BIOMETRIC_WEAK_EVER_CHOSEN_KEY, |
| 415 | LockPatternUtils.LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, |
| 416 | LockPatternUtils.PASSWORD_HISTORY_KEY, |
| 417 | Secure.LOCK_PATTERN_ENABLED, |
| 418 | Secure.LOCK_BIOMETRIC_WEAK_FLAGS, |
| 419 | Secure.LOCK_PATTERN_VISIBLE, |
| 420 | Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 421 | }; |
| 422 | |
Jim Miller | 2d8ecf9d | 2013-04-22 17:17:03 -0700 | [diff] [blame] | 423 | // These are protected with a read permission |
| 424 | private static final String[] READ_PROFILE_PROTECTED_SETTINGS = new String[] { |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 425 | Secure.LOCK_SCREEN_OWNER_INFO_ENABLED, |
| 426 | Secure.LOCK_SCREEN_OWNER_INFO |
| 427 | }; |
Paul Lawrence | 945490c | 2014-03-27 16:37:28 +0000 | [diff] [blame] | 428 | |
| 429 | private IMountService getMountService() { |
| 430 | final IBinder service = ServiceManager.getService("mount"); |
| 431 | if (service != null) { |
| 432 | return IMountService.Stub.asInterface(service); |
| 433 | } |
| 434 | return null; |
| 435 | } |
Amith Yamasani | 52c489c | 2012-03-28 11:42:42 -0700 | [diff] [blame] | 436 | } |