Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +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.trust; |
| 18 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 19 | import android.Manifest; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 20 | import android.annotation.UserIdInt; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 22 | import android.app.admin.DevicePolicyManager; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 23 | import android.hardware.biometrics.BiometricSourceType; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 24 | import android.app.trust.ITrustListener; |
| 25 | import android.app.trust.ITrustManager; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
| 34 | import android.content.pm.UserInfo; |
| 35 | import android.content.res.Resources; |
| 36 | import android.content.res.TypedArray; |
| 37 | import android.content.res.XmlResourceParser; |
| 38 | import android.graphics.drawable.Drawable; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 39 | import android.os.Binder; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 40 | import android.os.Build; |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 41 | import android.os.DeadObjectException; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.IBinder; |
| 44 | import android.os.Message; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 45 | import android.os.PersistableBundle; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 46 | import android.os.RemoteException; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 47 | import android.os.SystemClock; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 48 | import android.os.UserHandle; |
| 49 | import android.os.UserManager; |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 50 | import android.os.storage.StorageManager; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 51 | import android.provider.Settings; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 52 | import android.service.trust.TrustAgentService; |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 53 | import android.text.TextUtils; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 54 | import android.util.ArraySet; |
| 55 | import android.util.AttributeSet; |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 56 | import android.util.Log; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 57 | import android.util.Slog; |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 58 | import android.util.SparseBooleanArray; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 59 | import android.util.Xml; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 60 | import android.view.IWindowManager; |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 61 | import android.view.WindowManagerGlobal; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 62 | import com.android.internal.annotations.GuardedBy; |
| 63 | import com.android.internal.content.PackageMonitor; |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 64 | import com.android.internal.policy.IKeyguardDismissCallback; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 65 | import com.android.internal.util.DumpUtils; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 66 | import com.android.internal.widget.LockPatternUtils; |
| 67 | import com.android.server.SystemService; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 69 | import java.io.IOException; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 70 | import java.io.PrintWriter; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 71 | import java.util.ArrayList; |
| 72 | import java.util.List; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 73 | import org.xmlpull.v1.XmlPullParser; |
| 74 | import org.xmlpull.v1.XmlPullParserException; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Manages trust agents and trust listeners. |
| 78 | * |
| 79 | * It is responsible for binding to the enabled {@link android.service.trust.TrustAgentService}s |
| 80 | * of each user and notifies them about events that are relevant to them. |
| 81 | * It start and stops them based on the value of |
| 82 | * {@link com.android.internal.widget.LockPatternUtils#getEnabledTrustAgents(int)}. |
| 83 | * |
| 84 | * It also keeps a set of {@link android.app.trust.ITrustListener}s that are notified whenever the |
| 85 | * trust state changes for any user. |
| 86 | * |
| 87 | * Trust state and the setting of enabled agents is kept per user and each user has its own |
| 88 | * instance of a {@link android.service.trust.TrustAgentService}. |
| 89 | */ |
| 90 | public class TrustManagerService extends SystemService { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 91 | private static final String TAG = "TrustManagerService"; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 92 | static final boolean DEBUG = Build.IS_DEBUGGABLE && Log.isLoggable(TAG, Log.VERBOSE); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 93 | |
| 94 | private static final Intent TRUST_AGENT_INTENT = |
| 95 | new Intent(TrustAgentService.SERVICE_INTERFACE); |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 96 | private static final String PERMISSION_PROVIDE_AGENT = Manifest.permission.PROVIDE_TRUST_AGENT; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 97 | |
| 98 | private static final int MSG_REGISTER_LISTENER = 1; |
| 99 | private static final int MSG_UNREGISTER_LISTENER = 2; |
| 100 | private static final int MSG_DISPATCH_UNLOCK_ATTEMPT = 3; |
| 101 | private static final int MSG_ENABLED_AGENTS_CHANGED = 4; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 102 | private static final int MSG_KEYGUARD_SHOWING_CHANGED = 6; |
| 103 | private static final int MSG_START_USER = 7; |
| 104 | private static final int MSG_CLEANUP_USER = 8; |
| 105 | private static final int MSG_SWITCH_USER = 9; |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 106 | private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 10; |
| 107 | private static final int MSG_UNLOCK_USER = 11; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 108 | private static final int MSG_STOP_USER = 12; |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 109 | private static final int MSG_DISPATCH_UNLOCK_LOCKOUT = 13; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 110 | private static final int MSG_REFRESH_DEVICE_LOCKED_FOR_USER = 14; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 111 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 112 | private static final int TRUST_USUALLY_MANAGED_FLUSH_DELAY = 2 * 60 * 1000; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 113 | |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 114 | private final ArraySet<AgentInfo> mActiveAgents = new ArraySet<>(); |
| 115 | private final ArrayList<ITrustListener> mTrustListeners = new ArrayList<>(); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 116 | private final Receiver mReceiver = new Receiver(); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 117 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 118 | /* package */ final TrustArchive mArchive = new TrustArchive(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 119 | private final Context mContext; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 120 | private final LockPatternUtils mLockPatternUtils; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 121 | private final UserManager mUserManager; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 122 | private final ActivityManager mActivityManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 123 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 124 | @GuardedBy("mUserIsTrusted") |
| 125 | private final SparseBooleanArray mUserIsTrusted = new SparseBooleanArray(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 126 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 127 | @GuardedBy("mDeviceLockedForUser") |
| 128 | private final SparseBooleanArray mDeviceLockedForUser = new SparseBooleanArray(); |
| 129 | |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 130 | @GuardedBy("mTrustUsuallyManagedForUser") |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 131 | private final SparseBooleanArray mTrustUsuallyManagedForUser = new SparseBooleanArray(); |
| 132 | |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 133 | // set to true only if user can skip bouncer |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 134 | @GuardedBy("mUsersUnlockedByBiometric") |
| 135 | private final SparseBooleanArray mUsersUnlockedByBiometric = new SparseBooleanArray(); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 136 | |
Rakesh Iyer | a7aa4d6 | 2016-01-19 17:27:23 -0800 | [diff] [blame] | 137 | private final StrongAuthTracker mStrongAuthTracker; |
| 138 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 139 | private boolean mTrustAgentsCanRun = false; |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 140 | private int mCurrentUser = UserHandle.USER_SYSTEM; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 141 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 142 | public TrustManagerService(Context context) { |
| 143 | super(context); |
| 144 | mContext = context; |
| 145 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 146 | mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 147 | mLockPatternUtils = new LockPatternUtils(context); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 148 | mStrongAuthTracker = new StrongAuthTracker(context); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onStart() { |
| 153 | publishBinderService(Context.TRUST_SERVICE, mService); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onBootPhase(int phase) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 158 | if (isSafeMode()) { |
| 159 | // No trust agents in safe mode. |
| 160 | return; |
| 161 | } |
| 162 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 163 | mPackageMonitor.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 164 | mReceiver.register(mContext); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 165 | mLockPatternUtils.registerStrongAuthTracker(mStrongAuthTracker); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 166 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 167 | mTrustAgentsCanRun = true; |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 168 | refreshAgentList(UserHandle.USER_ALL); |
Xiaohui Chen | 605733b | 2016-05-19 16:39:09 -0700 | [diff] [blame] | 169 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 170 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 171 | maybeEnableFactoryTrustAgents(mLockPatternUtils, UserHandle.USER_SYSTEM); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | // Agent management |
| 176 | |
| 177 | private static final class AgentInfo { |
| 178 | CharSequence label; |
| 179 | Drawable icon; |
| 180 | ComponentName component; // service that implements ITrustAgent |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 181 | SettingsAttrs settings; // setting to launch to modify agent. |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 182 | TrustAgentWrapper agent; |
| 183 | int userId; |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(Object other) { |
| 187 | if (!(other instanceof AgentInfo)) { |
| 188 | return false; |
| 189 | } |
| 190 | AgentInfo o = (AgentInfo) other; |
| 191 | return component.equals(o.component) && userId == o.userId; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int hashCode() { |
| 196 | return component.hashCode() * 31 + userId; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private void updateTrustAll() { |
| 201 | List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 202 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 203 | updateTrust(userInfo.id, 0); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 207 | public void updateTrust(int userId, int flags) { |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 208 | boolean managed = aggregateIsTrustManaged(userId); |
| 209 | dispatchOnTrustManagedChanged(managed, userId); |
| 210 | if (mStrongAuthTracker.isTrustAllowedForUser(userId) |
| 211 | && isTrustUsuallyManagedInternal(userId) != managed) { |
| 212 | updateTrustUsuallyManaged(userId, managed); |
| 213 | } |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 214 | boolean trusted = aggregateIsTrusted(userId); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 215 | boolean changed; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 216 | synchronized (mUserIsTrusted) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 217 | changed = mUserIsTrusted.get(userId) != trusted; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 218 | mUserIsTrusted.put(userId, trusted); |
| 219 | } |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 220 | dispatchOnTrustChanged(trusted, userId, flags); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 221 | if (changed) { |
| 222 | refreshDeviceLockedForUser(userId); |
| 223 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 226 | private void updateTrustUsuallyManaged(int userId, boolean managed) { |
| 227 | synchronized (mTrustUsuallyManagedForUser) { |
| 228 | mTrustUsuallyManagedForUser.put(userId, managed); |
| 229 | } |
| 230 | // Wait a few minutes before committing to flash, in case the trust agent is transiently not |
| 231 | // managing trust (crashed, needs to acknowledge DPM restrictions, etc). |
| 232 | mHandler.removeMessages(MSG_FLUSH_TRUST_USUALLY_MANAGED); |
| 233 | mHandler.sendMessageDelayed( |
| 234 | mHandler.obtainMessage(MSG_FLUSH_TRUST_USUALLY_MANAGED), |
| 235 | TRUST_USUALLY_MANAGED_FLUSH_DELAY); |
| 236 | } |
| 237 | |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 238 | public long addEscrowToken(byte[] token, int userId) { |
| 239 | return mLockPatternUtils.addEscrowToken(token, userId); |
| 240 | } |
| 241 | |
| 242 | public boolean removeEscrowToken(long handle, int userId) { |
| 243 | return mLockPatternUtils.removeEscrowToken(handle, userId); |
| 244 | } |
| 245 | |
| 246 | public boolean isEscrowTokenActive(long handle, int userId) { |
| 247 | return mLockPatternUtils.isEscrowTokenActive(handle, userId); |
| 248 | } |
| 249 | |
| 250 | public void unlockUserWithToken(long handle, byte[] token, int userId) { |
| 251 | mLockPatternUtils.unlockUserWithToken(handle, token, userId); |
| 252 | } |
| 253 | |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 254 | void showKeyguardErrorMessage(CharSequence message) { |
| 255 | dispatchOnTrustError(message); |
| 256 | } |
| 257 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 258 | void refreshAgentList(int userIdOrAll) { |
| 259 | if (DEBUG) Slog.d(TAG, "refreshAgentList(" + userIdOrAll + ")"); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 260 | if (!mTrustAgentsCanRun) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 261 | return; |
| 262 | } |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 263 | if (userIdOrAll != UserHandle.USER_ALL && userIdOrAll < UserHandle.USER_SYSTEM) { |
| 264 | Log.e(TAG, "refreshAgentList(userId=" + userIdOrAll + "): Invalid user handle," |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 265 | + " must be USER_ALL or a specific user.", new Throwable("here")); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 266 | userIdOrAll = UserHandle.USER_ALL; |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 267 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 268 | PackageManager pm = mContext.getPackageManager(); |
| 269 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 270 | List<UserInfo> userInfos; |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 271 | if (userIdOrAll == UserHandle.USER_ALL) { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 272 | userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 273 | } else { |
| 274 | userInfos = new ArrayList<>(); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 275 | userInfos.add(mUserManager.getUserInfo(userIdOrAll)); |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 276 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 277 | LockPatternUtils lockPatternUtils = mLockPatternUtils; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 278 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 279 | ArraySet<AgentInfo> obsoleteAgents = new ArraySet<>(); |
| 280 | obsoleteAgents.addAll(mActiveAgents); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 281 | |
| 282 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 283 | if (userInfo == null || userInfo.partial || !userInfo.isEnabled() |
| 284 | || userInfo.guestToRemove) continue; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 285 | if (!userInfo.supportsSwitchToByUser()) { |
| 286 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 287 | + ": switchToByUser=false"); |
| 288 | continue; |
| 289 | } |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 290 | if (!mActivityManager.isUserRunning(userInfo.id)) { |
| 291 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 292 | + ": user not started"); |
| 293 | continue; |
| 294 | } |
| 295 | if (!lockPatternUtils.isSecure(userInfo.id)) { |
| 296 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 297 | + ": no secure credential"); |
| 298 | continue; |
| 299 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 300 | |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 301 | DevicePolicyManager dpm = lockPatternUtils.getDevicePolicyManager(); |
| 302 | int disabledFeatures = dpm.getKeyguardDisabledFeatures(null, userInfo.id); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 303 | final boolean disableTrustAgents = |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 304 | (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0; |
| 305 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 306 | List<ComponentName> enabledAgents = lockPatternUtils.getEnabledTrustAgents(userInfo.id); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 307 | if (enabledAgents == null) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 308 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 309 | + ": no agents enabled by user"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 310 | continue; |
| 311 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 312 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userInfo.id); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 313 | for (ResolveInfo resolveInfo : resolveInfos) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 314 | ComponentName name = getComponentName(resolveInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 315 | |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 316 | if (!enabledAgents.contains(name)) { |
| 317 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping " |
| 318 | + name.flattenToShortString() + " u"+ userInfo.id |
| 319 | + ": not enabled by user"); |
| 320 | continue; |
| 321 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 322 | if (disableTrustAgents) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 323 | List<PersistableBundle> config = |
| 324 | dpm.getTrustAgentConfiguration(null /* admin */, name, userInfo.id); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 325 | // Disable agent if no features are enabled. |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 326 | if (config == null || config.isEmpty()) { |
| 327 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping " |
| 328 | + name.flattenToShortString() + " u"+ userInfo.id |
| 329 | + ": not allowed by DPM"); |
| 330 | continue; |
| 331 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 332 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 333 | AgentInfo agentInfo = new AgentInfo(); |
| 334 | agentInfo.component = name; |
| 335 | agentInfo.userId = userInfo.id; |
| 336 | if (!mActiveAgents.contains(agentInfo)) { |
| 337 | agentInfo.label = resolveInfo.loadLabel(pm); |
| 338 | agentInfo.icon = resolveInfo.loadIcon(pm); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 339 | agentInfo.settings = getSettingsAttrs(pm, resolveInfo); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 340 | } else { |
| 341 | int index = mActiveAgents.indexOf(agentInfo); |
| 342 | agentInfo = mActiveAgents.valueAt(index); |
| 343 | } |
| 344 | |
| 345 | boolean directUnlock = resolveInfo.serviceInfo.directBootAware |
| 346 | && agentInfo.settings.canUnlockProfile; |
| 347 | |
| 348 | if (directUnlock) { |
| 349 | if (DEBUG) Slog.d(TAG, "refreshAgentList: trustagent " + name |
| 350 | + "of user " + userInfo.id + "can unlock user profile."); |
| 351 | } |
| 352 | |
Jeff Sharkey | a65e649 | 2017-06-21 13:45:11 -0600 | [diff] [blame] | 353 | if (!mUserManager.isUserUnlockingOrUnlocked(userInfo.id) |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 354 | && !directUnlock) { |
| 355 | if (DEBUG) Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 356 | + "'s trust agent " + name + ": FBE still locked and " |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 357 | + " the agent cannot unlock user profile."); |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | if (!mStrongAuthTracker.canAgentsRunForUser(userInfo.id)) { |
| 362 | int flag = mStrongAuthTracker.getStrongAuthForUser(userInfo.id); |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 363 | if (flag != StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT) { |
| 364 | if (flag != StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT |
| 365 | || !directUnlock) { |
| 366 | if (DEBUG) |
| 367 | Slog.d(TAG, "refreshAgentList: skipping user " + userInfo.id |
| 368 | + ": prevented by StrongAuthTracker = 0x" |
| 369 | + Integer.toHexString(mStrongAuthTracker.getStrongAuthForUser( |
| 370 | userInfo.id))); |
| 371 | continue; |
| 372 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Adrian Roos | b884bb2 | 2017-05-03 11:33:51 -0700 | [diff] [blame] | 376 | if (agentInfo.agent == null) { |
| 377 | agentInfo.agent = new TrustAgentWrapper(mContext, this, |
| 378 | new Intent().setComponent(name), userInfo.getUserHandle()); |
| 379 | } |
| 380 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 381 | if (!mActiveAgents.contains(agentInfo)) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 382 | mActiveAgents.add(agentInfo); |
| 383 | } else { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 384 | obsoleteAgents.remove(agentInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | boolean trustMayHaveChanged = false; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 390 | for (int i = 0; i < obsoleteAgents.size(); i++) { |
| 391 | AgentInfo info = obsoleteAgents.valueAt(i); |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 392 | if (userIdOrAll == UserHandle.USER_ALL || userIdOrAll == info.userId) { |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 393 | if (info.agent.isManagingTrust()) { |
| 394 | trustMayHaveChanged = true; |
| 395 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 396 | info.agent.destroy(); |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 397 | mActiveAgents.remove(info); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 398 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (trustMayHaveChanged) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 402 | if (userIdOrAll == UserHandle.USER_ALL) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 403 | updateTrustAll(); |
| 404 | } else { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 405 | updateTrust(userIdOrAll, 0); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 406 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 410 | boolean isDeviceLockedInner(int userId) { |
| 411 | synchronized (mDeviceLockedForUser) { |
| 412 | return mDeviceLockedForUser.get(userId, true); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | private void refreshDeviceLockedForUser(int userId) { |
Xiaohui Chen | 09e0291 | 2015-08-05 09:44:56 -0700 | [diff] [blame] | 417 | if (userId != UserHandle.USER_ALL && userId < UserHandle.USER_SYSTEM) { |
Adrian Roos | 7e2e40e | 2014-11-21 15:27:12 +0100 | [diff] [blame] | 418 | Log.e(TAG, "refreshDeviceLockedForUser(userId=" + userId + "): Invalid user handle," |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 419 | + " must be USER_ALL or a specific user.", new Throwable("here")); |
| 420 | userId = UserHandle.USER_ALL; |
| 421 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 422 | List<UserInfo> userInfos; |
| 423 | if (userId == UserHandle.USER_ALL) { |
| 424 | userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 425 | } else { |
| 426 | userInfos = new ArrayList<>(); |
| 427 | userInfos.add(mUserManager.getUserInfo(userId)); |
| 428 | } |
| 429 | |
| 430 | IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 431 | |
| 432 | for (int i = 0; i < userInfos.size(); i++) { |
| 433 | UserInfo info = userInfos.get(i); |
| 434 | |
| 435 | if (info == null || info.partial || !info.isEnabled() || info.guestToRemove |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 436 | || !info.supportsSwitchToByUser()) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 437 | continue; |
| 438 | } |
| 439 | |
| 440 | int id = info.id; |
| 441 | boolean secure = mLockPatternUtils.isSecure(id); |
| 442 | boolean trusted = aggregateIsTrusted(id); |
| 443 | boolean showingKeyguard = true; |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 444 | boolean biometricAuthenticated = false; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 445 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 446 | if (mCurrentUser == id) { |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 447 | synchronized(mUsersUnlockedByBiometric) { |
| 448 | biometricAuthenticated = mUsersUnlockedByBiometric.get(id, false); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 449 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 450 | try { |
| 451 | showingKeyguard = wm.isKeyguardLocked(); |
| 452 | } catch (RemoteException e) { |
| 453 | } |
| 454 | } |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 455 | boolean deviceLocked = secure && showingKeyguard && !trusted && |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 456 | !biometricAuthenticated; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 457 | setDeviceLockedForUser(id, deviceLocked); |
| 458 | } |
| 459 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 460 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 461 | private void setDeviceLockedForUser(@UserIdInt int userId, boolean locked) { |
| 462 | final boolean changed; |
| 463 | synchronized (mDeviceLockedForUser) { |
| 464 | changed = isDeviceLockedInner(userId) != locked; |
| 465 | mDeviceLockedForUser.put(userId, locked); |
| 466 | } |
| 467 | if (changed) { |
| 468 | dispatchDeviceLocked(userId, locked); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | private void dispatchDeviceLocked(int userId, boolean isLocked) { |
| 473 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 474 | AgentInfo agent = mActiveAgents.valueAt(i); |
| 475 | if (agent.userId == userId) { |
| 476 | if (isLocked) { |
| 477 | agent.agent.onDeviceLocked(); |
| 478 | } else{ |
| 479 | agent.agent.onDeviceUnlocked(); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 485 | void updateDevicePolicyFeatures() { |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 486 | boolean changed = false; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 487 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 488 | AgentInfo info = mActiveAgents.valueAt(i); |
| 489 | if (info.agent.isConnected()) { |
| 490 | info.agent.updateDevicePolicyFeatures(); |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 491 | changed = true; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 492 | } |
| 493 | } |
Adrian Roos | 9d6fc92 | 2016-08-10 17:09:55 -0700 | [diff] [blame] | 494 | if (changed) { |
| 495 | mArchive.logDevicePolicyChanged(); |
| 496 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 499 | private void removeAgentsOfPackage(String packageName) { |
| 500 | boolean trustMayHaveChanged = false; |
| 501 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 502 | AgentInfo info = mActiveAgents.valueAt(i); |
| 503 | if (packageName.equals(info.component.getPackageName())) { |
| 504 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 505 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 506 | trustMayHaveChanged = true; |
| 507 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 508 | info.agent.destroy(); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 509 | mActiveAgents.removeAt(i); |
| 510 | } |
| 511 | } |
| 512 | if (trustMayHaveChanged) { |
| 513 | updateTrustAll(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | public void resetAgent(ComponentName name, int userId) { |
| 518 | boolean trustMayHaveChanged = false; |
| 519 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 520 | AgentInfo info = mActiveAgents.valueAt(i); |
| 521 | if (name.equals(info.component) && userId == info.userId) { |
| 522 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 523 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 524 | trustMayHaveChanged = true; |
| 525 | } |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 526 | info.agent.destroy(); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 527 | mActiveAgents.removeAt(i); |
| 528 | } |
| 529 | } |
| 530 | if (trustMayHaveChanged) { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 531 | updateTrust(userId, 0); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 532 | } |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 533 | refreshAgentList(userId); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 536 | private SettingsAttrs getSettingsAttrs(PackageManager pm, ResolveInfo resolveInfo) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 537 | if (resolveInfo == null || resolveInfo.serviceInfo == null |
| 538 | || resolveInfo.serviceInfo.metaData == null) return null; |
| 539 | String cn = null; |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 540 | boolean canUnlockProfile = false; |
| 541 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 542 | XmlResourceParser parser = null; |
| 543 | Exception caughtException = null; |
| 544 | try { |
| 545 | parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, |
| 546 | TrustAgentService.TRUST_AGENT_META_DATA); |
| 547 | if (parser == null) { |
| 548 | Slog.w(TAG, "Can't find " + TrustAgentService.TRUST_AGENT_META_DATA + " meta-data"); |
| 549 | return null; |
| 550 | } |
| 551 | Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo); |
| 552 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 553 | int type; |
| 554 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 555 | && type != XmlPullParser.START_TAG) { |
| 556 | // Drain preamble. |
| 557 | } |
| 558 | String nodeName = parser.getName(); |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 559 | if (!"trust-agent".equals(nodeName)) { |
| 560 | Slog.w(TAG, "Meta-data does not start with trust-agent tag"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 561 | return null; |
| 562 | } |
| 563 | TypedArray sa = res |
| 564 | .obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent); |
| 565 | cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity); |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 566 | canUnlockProfile = sa.getBoolean( |
| 567 | com.android.internal.R.styleable.TrustAgent_unlockProfile, false); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 568 | sa.recycle(); |
| 569 | } catch (PackageManager.NameNotFoundException e) { |
| 570 | caughtException = e; |
| 571 | } catch (IOException e) { |
| 572 | caughtException = e; |
| 573 | } catch (XmlPullParserException e) { |
| 574 | caughtException = e; |
| 575 | } finally { |
| 576 | if (parser != null) parser.close(); |
| 577 | } |
| 578 | if (caughtException != null) { |
| 579 | Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException); |
| 580 | return null; |
| 581 | } |
| 582 | if (cn == null) { |
| 583 | return null; |
| 584 | } |
| 585 | if (cn.indexOf('/') < 0) { |
| 586 | cn = resolveInfo.serviceInfo.packageName + "/" + cn; |
| 587 | } |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 588 | return new SettingsAttrs(ComponentName.unflattenFromString(cn), canUnlockProfile); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | private ComponentName getComponentName(ResolveInfo resolveInfo) { |
| 592 | if (resolveInfo == null || resolveInfo.serviceInfo == null) return null; |
| 593 | return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); |
| 594 | } |
| 595 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 596 | private void maybeEnableFactoryTrustAgents(LockPatternUtils utils, int userId) { |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 597 | if (0 != Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 598 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 0, userId)) { |
| 599 | return; |
| 600 | } |
| 601 | PackageManager pm = mContext.getPackageManager(); |
| 602 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userId); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 603 | ComponentName defaultAgent = getDefaultFactoryTrustAgent(mContext); |
| 604 | boolean shouldUseDefaultAgent = defaultAgent != null; |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 605 | ArraySet<ComponentName> discoveredAgents = new ArraySet<>(); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 606 | |
| 607 | if (shouldUseDefaultAgent) { |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 608 | discoveredAgents.add(defaultAgent); |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 609 | Log.i(TAG, "Enabling " + defaultAgent + " because it is a default agent."); |
| 610 | } else { // A default agent is not set; perform regular trust agent discovery |
| 611 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 612 | ComponentName componentName = getComponentName(resolveInfo); |
| 613 | int applicationInfoFlags = resolveInfo.serviceInfo.applicationInfo.flags; |
| 614 | if ((applicationInfoFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 615 | Log.i(TAG, "Leaving agent " + componentName + " disabled because package " |
| 616 | + "is not a system package."); |
| 617 | continue; |
| 618 | } |
| 619 | discoveredAgents.add(componentName); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 620 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 621 | } |
Adrian Roos | 2f19ad4 | 2017-07-05 16:09:22 +0200 | [diff] [blame] | 622 | |
| 623 | List<ComponentName> previouslyEnabledAgents = utils.getEnabledTrustAgents(userId); |
| 624 | if (previouslyEnabledAgents != null) { |
| 625 | discoveredAgents.addAll(previouslyEnabledAgents); |
| 626 | } |
| 627 | utils.setEnabledTrustAgents(discoveredAgents, userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 628 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 629 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 1, userId); |
| 630 | } |
| 631 | |
Zachary Iqbal | 666484d | 2017-03-31 19:10:09 -0700 | [diff] [blame] | 632 | /** |
| 633 | * Returns the {@link ComponentName} for the default trust agent, or {@code null} if there |
| 634 | * is no trust agent set. |
| 635 | */ |
| 636 | private static ComponentName getDefaultFactoryTrustAgent(Context context) { |
| 637 | String defaultTrustAgent = context.getResources() |
| 638 | .getString(com.android.internal.R.string.config_defaultTrustAgent); |
| 639 | if (TextUtils.isEmpty(defaultTrustAgent)) { |
| 640 | return null; |
| 641 | } |
| 642 | return ComponentName.unflattenFromString(defaultTrustAgent); |
| 643 | } |
| 644 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 645 | private List<ResolveInfo> resolveAllowedTrustAgents(PackageManager pm, int userId) { |
| 646 | List<ResolveInfo> resolveInfos = pm.queryIntentServicesAsUser(TRUST_AGENT_INTENT, |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 647 | PackageManager.GET_META_DATA | |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 648 | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
| 649 | userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 650 | ArrayList<ResolveInfo> allowedAgents = new ArrayList<>(resolveInfos.size()); |
| 651 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 652 | if (resolveInfo.serviceInfo == null) continue; |
| 653 | if (resolveInfo.serviceInfo.applicationInfo == null) continue; |
| 654 | String packageName = resolveInfo.serviceInfo.packageName; |
| 655 | if (pm.checkPermission(PERMISSION_PROVIDE_AGENT, packageName) |
| 656 | != PackageManager.PERMISSION_GRANTED) { |
| 657 | ComponentName name = getComponentName(resolveInfo); |
| 658 | Log.w(TAG, "Skipping agent " + name + " because package does not have" |
| 659 | + " permission " + PERMISSION_PROVIDE_AGENT + "."); |
| 660 | continue; |
| 661 | } |
| 662 | allowedAgents.add(resolveInfo); |
| 663 | } |
| 664 | return allowedAgents; |
| 665 | } |
| 666 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 667 | // Agent dispatch and aggregation |
| 668 | |
| 669 | private boolean aggregateIsTrusted(int userId) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 670 | if (!mStrongAuthTracker.isTrustAllowedForUser(userId)) { |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 671 | return false; |
| 672 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 673 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 674 | AgentInfo info = mActiveAgents.valueAt(i); |
| 675 | if (info.userId == userId) { |
| 676 | if (info.agent.isTrusted()) { |
| 677 | return true; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | return false; |
| 682 | } |
| 683 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 684 | private boolean aggregateIsTrustManaged(int userId) { |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 685 | if (!mStrongAuthTracker.isTrustAllowedForUser(userId)) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 686 | return false; |
| 687 | } |
| 688 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 689 | AgentInfo info = mActiveAgents.valueAt(i); |
| 690 | if (info.userId == userId) { |
| 691 | if (info.agent.isManagingTrust()) { |
| 692 | return true; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 699 | private void dispatchUnlockAttempt(boolean successful, int userId) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 700 | if (successful) { |
| 701 | mStrongAuthTracker.allowTrustFromUnlock(userId); |
| 702 | } |
| 703 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 704 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 705 | AgentInfo info = mActiveAgents.valueAt(i); |
| 706 | if (info.userId == userId) { |
| 707 | info.agent.onUnlockAttempt(successful); |
| 708 | } |
| 709 | } |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 712 | private void dispatchUnlockLockout(int timeoutMs, int userId) { |
| 713 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 714 | AgentInfo info = mActiveAgents.valueAt(i); |
| 715 | if (info.userId == userId) { |
| 716 | info.agent.onUnlockLockout(timeoutMs); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 721 | // Listeners |
| 722 | |
| 723 | private void addListener(ITrustListener listener) { |
| 724 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 725 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
| 726 | return; |
| 727 | } |
| 728 | } |
| 729 | mTrustListeners.add(listener); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 730 | updateTrustAll(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | private void removeListener(ITrustListener listener) { |
| 734 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 735 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
Jay Civelli | 979a32e | 2014-07-18 16:47:36 -0700 | [diff] [blame] | 736 | mTrustListeners.remove(i); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 737 | return; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 742 | private void dispatchOnTrustChanged(boolean enabled, int userId, int flags) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 743 | if (DEBUG) { |
| 744 | Log.i(TAG, "onTrustChanged(" + enabled + ", " + userId + ", 0x" |
| 745 | + Integer.toHexString(flags) + ")"); |
| 746 | } |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 747 | if (!enabled) flags = 0; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 748 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 749 | try { |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 750 | mTrustListeners.get(i).onTrustChanged(enabled, userId, flags); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 751 | } catch (DeadObjectException e) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 752 | Slog.d(TAG, "Removing dead TrustListener."); |
| 753 | mTrustListeners.remove(i); |
| 754 | i--; |
| 755 | } catch (RemoteException e) { |
| 756 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | private void dispatchOnTrustManagedChanged(boolean managed, int userId) { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 762 | if (DEBUG) { |
| 763 | Log.i(TAG, "onTrustManagedChanged(" + managed + ", " + userId + ")"); |
| 764 | } |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 765 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 766 | try { |
| 767 | mTrustListeners.get(i).onTrustManagedChanged(managed, userId); |
| 768 | } catch (DeadObjectException e) { |
| 769 | Slog.d(TAG, "Removing dead TrustListener."); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 770 | mTrustListeners.remove(i); |
| 771 | i--; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 772 | } catch (RemoteException e) { |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 773 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 778 | private void dispatchOnTrustError(CharSequence message) { |
| 779 | if (DEBUG) { |
| 780 | Log.i(TAG, "onTrustError(" + message + ")"); |
| 781 | } |
| 782 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 783 | try { |
| 784 | mTrustListeners.get(i).onTrustError(message); |
| 785 | } catch (DeadObjectException e) { |
| 786 | Slog.d(TAG, "Removing dead TrustListener."); |
| 787 | mTrustListeners.remove(i); |
| 788 | i--; |
| 789 | } catch (RemoteException e) { |
| 790 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 795 | // User lifecycle |
| 796 | |
| 797 | @Override |
| 798 | public void onStartUser(int userId) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 799 | mHandler.obtainMessage(MSG_START_USER, userId, 0, null).sendToTarget(); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | @Override |
| 803 | public void onCleanupUser(int userId) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 804 | mHandler.obtainMessage(MSG_CLEANUP_USER, userId, 0, null).sendToTarget(); |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public void onSwitchUser(int userId) { |
| 809 | mHandler.obtainMessage(MSG_SWITCH_USER, userId, 0, null).sendToTarget(); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 812 | @Override |
| 813 | public void onUnlockUser(int userId) { |
| 814 | mHandler.obtainMessage(MSG_UNLOCK_USER, userId, 0, null).sendToTarget(); |
| 815 | } |
| 816 | |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 817 | @Override |
| 818 | public void onStopUser(@UserIdInt int userId) { |
| 819 | mHandler.obtainMessage(MSG_STOP_USER, userId, 0, null).sendToTarget(); |
| 820 | } |
| 821 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 822 | // Plumbing |
| 823 | |
| 824 | private final IBinder mService = new ITrustManager.Stub() { |
| 825 | @Override |
| 826 | public void reportUnlockAttempt(boolean authenticated, int userId) throws RemoteException { |
| 827 | enforceReportPermission(); |
| 828 | mHandler.obtainMessage(MSG_DISPATCH_UNLOCK_ATTEMPT, authenticated ? 1 : 0, userId) |
| 829 | .sendToTarget(); |
| 830 | } |
| 831 | |
| 832 | @Override |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 833 | public void reportUnlockLockout(int timeoutMs, int userId) throws RemoteException { |
| 834 | enforceReportPermission(); |
| 835 | mHandler.obtainMessage(MSG_DISPATCH_UNLOCK_LOCKOUT, timeoutMs, userId) |
| 836 | .sendToTarget(); |
| 837 | } |
| 838 | |
| 839 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 840 | public void reportEnabledTrustAgentsChanged(int userId) throws RemoteException { |
| 841 | enforceReportPermission(); |
| 842 | // coalesce refresh messages. |
| 843 | mHandler.removeMessages(MSG_ENABLED_AGENTS_CHANGED); |
| 844 | mHandler.sendEmptyMessage(MSG_ENABLED_AGENTS_CHANGED); |
| 845 | } |
| 846 | |
| 847 | @Override |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 848 | public void reportKeyguardShowingChanged() throws RemoteException { |
| 849 | enforceReportPermission(); |
| 850 | // coalesce refresh messages. |
| 851 | mHandler.removeMessages(MSG_KEYGUARD_SHOWING_CHANGED); |
| 852 | mHandler.sendEmptyMessage(MSG_KEYGUARD_SHOWING_CHANGED); |
Jorim Jaggi | 5277dea | 2017-05-18 02:05:29 +0200 | [diff] [blame] | 853 | |
| 854 | // Make sure handler processes the message before returning, such that isDeviceLocked |
| 855 | // after this call will retrieve the correct value. |
| 856 | mHandler.runWithScissors(() -> {}, 0); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 860 | public void registerTrustListener(ITrustListener trustListener) throws RemoteException { |
| 861 | enforceListenerPermission(); |
| 862 | mHandler.obtainMessage(MSG_REGISTER_LISTENER, trustListener).sendToTarget(); |
| 863 | } |
| 864 | |
| 865 | @Override |
| 866 | public void unregisterTrustListener(ITrustListener trustListener) throws RemoteException { |
| 867 | enforceListenerPermission(); |
| 868 | mHandler.obtainMessage(MSG_UNREGISTER_LISTENER, trustListener).sendToTarget(); |
| 869 | } |
| 870 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 871 | @Override |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 872 | public boolean isDeviceLocked(int userId) throws RemoteException { |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 873 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 874 | false /* allowAll */, true /* requireFull */, "isDeviceLocked", null); |
Adrian Roos | 50bfeec | 2014-11-20 16:21:11 +0100 | [diff] [blame] | 875 | |
Clara Bayarri | 078e91b | 2016-01-15 16:57:35 +0000 | [diff] [blame] | 876 | long token = Binder.clearCallingIdentity(); |
| 877 | try { |
| 878 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
| 879 | userId = resolveProfileParent(userId); |
| 880 | } |
| 881 | return isDeviceLockedInner(userId); |
| 882 | } finally { |
| 883 | Binder.restoreCallingIdentity(token); |
| 884 | } |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 885 | } |
| 886 | |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 887 | @Override |
| 888 | public boolean isDeviceSecure(int userId) throws RemoteException { |
| 889 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
| 890 | false /* allowAll */, true /* requireFull */, "isDeviceSecure", null); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 891 | |
| 892 | long token = Binder.clearCallingIdentity(); |
| 893 | try { |
Clara Bayarri | 8d35de8 | 2016-01-12 17:29:29 +0000 | [diff] [blame] | 894 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
| 895 | userId = resolveProfileParent(userId); |
| 896 | } |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 897 | return mLockPatternUtils.isSecure(userId); |
Adrian Roos | 8289368 | 2015-04-02 16:17:46 +0200 | [diff] [blame] | 898 | } finally { |
| 899 | Binder.restoreCallingIdentity(token); |
| 900 | } |
| 901 | } |
| 902 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 903 | private void enforceReportPermission() { |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 904 | mContext.enforceCallingOrSelfPermission( |
| 905 | Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE, "reporting trust events"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | private void enforceListenerPermission() { |
| 909 | mContext.enforceCallingPermission(Manifest.permission.TRUST_LISTENER, |
| 910 | "register trust listener"); |
| 911 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 912 | |
| 913 | @Override |
| 914 | protected void dump(FileDescriptor fd, final PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 915 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 916 | if (isSafeMode()) { |
| 917 | fout.println("disabled because the system is in safe mode."); |
| 918 | return; |
| 919 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 920 | if (!mTrustAgentsCanRun) { |
| 921 | fout.println("disabled because the third-party apps can't run yet."); |
| 922 | return; |
| 923 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 924 | final List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 925 | mHandler.runWithScissors(new Runnable() { |
| 926 | @Override |
| 927 | public void run() { |
| 928 | fout.println("Trust manager state:"); |
| 929 | for (UserInfo user : userInfos) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 930 | dumpUser(fout, user, user.id == mCurrentUser); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | }, 1500); |
| 934 | } |
| 935 | |
| 936 | private void dumpUser(PrintWriter fout, UserInfo user, boolean isCurrent) { |
| 937 | fout.printf(" User \"%s\" (id=%d, flags=%#x)", |
| 938 | user.name, user.id, user.flags); |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 939 | if (!user.supportsSwitchToByUser()) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 940 | fout.println("(managed profile)"); |
| 941 | fout.println(" disabled because switching to this user is not possible."); |
| 942 | return; |
| 943 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 944 | if (isCurrent) { |
| 945 | fout.print(" (current)"); |
| 946 | } |
| 947 | fout.print(": trusted=" + dumpBool(aggregateIsTrusted(user.id))); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 948 | fout.print(", trustManaged=" + dumpBool(aggregateIsTrustManaged(user.id))); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 949 | fout.print(", deviceLocked=" + dumpBool(isDeviceLockedInner(user.id))); |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 950 | fout.print(", strongAuthRequired=" + dumpHex( |
| 951 | mStrongAuthTracker.getStrongAuthForUser(user.id))); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 952 | fout.println(); |
| 953 | fout.println(" Enabled agents:"); |
| 954 | boolean duplicateSimpleNames = false; |
| 955 | ArraySet<String> simpleNames = new ArraySet<String>(); |
| 956 | for (AgentInfo info : mActiveAgents) { |
| 957 | if (info.userId != user.id) { continue; } |
| 958 | boolean trusted = info.agent.isTrusted(); |
| 959 | fout.print(" "); fout.println(info.component.flattenToShortString()); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 960 | fout.print(" bound=" + dumpBool(info.agent.isBound())); |
| 961 | fout.print(", connected=" + dumpBool(info.agent.isConnected())); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 962 | fout.print(", managingTrust=" + dumpBool(info.agent.isManagingTrust())); |
| 963 | fout.print(", trusted=" + dumpBool(trusted)); |
| 964 | fout.println(); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 965 | if (trusted) { |
| 966 | fout.println(" message=\"" + info.agent.getMessage() + "\""); |
| 967 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 968 | if (!info.agent.isConnected()) { |
| 969 | String restartTime = TrustArchive.formatDuration( |
| 970 | info.agent.getScheduledRestartUptimeMillis() |
| 971 | - SystemClock.uptimeMillis()); |
| 972 | fout.println(" restartScheduledAt=" + restartTime); |
| 973 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 974 | if (!simpleNames.add(TrustArchive.getSimpleName(info.component))) { |
| 975 | duplicateSimpleNames = true; |
| 976 | } |
| 977 | } |
| 978 | fout.println(" Events:"); |
| 979 | mArchive.dump(fout, 50, user.id, " " /* linePrefix */, duplicateSimpleNames); |
| 980 | fout.println(); |
| 981 | } |
| 982 | |
| 983 | private String dumpBool(boolean b) { |
| 984 | return b ? "1" : "0"; |
| 985 | } |
Adrian Roos | b5e4722 | 2015-08-14 15:53:06 -0700 | [diff] [blame] | 986 | |
| 987 | private String dumpHex(int i) { |
| 988 | return "0x" + Integer.toHexString(i); |
| 989 | } |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 990 | |
| 991 | @Override |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 992 | public void setDeviceLockedForUser(int userId, boolean locked) { |
Clara Bayarri | 00a9b89 | 2016-01-13 16:17:09 +0000 | [diff] [blame] | 993 | enforceReportPermission(); |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 994 | final long identity = Binder.clearCallingIdentity(); |
| 995 | try { |
| 996 | if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) { |
| 997 | synchronized (mDeviceLockedForUser) { |
| 998 | mDeviceLockedForUser.put(userId, locked); |
| 999 | } |
| 1000 | if (locked) { |
| 1001 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1002 | ActivityManager.getService().notifyLockedProfile(userId); |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1003 | } catch (RemoteException e) { |
| 1004 | } |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1005 | } |
Robin Lee | 92b83c6 | 2016-08-31 13:27:51 +0100 | [diff] [blame] | 1006 | final Intent lockIntent = new Intent(Intent.ACTION_DEVICE_LOCKED_CHANGED); |
| 1007 | lockIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1008 | lockIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1009 | mContext.sendBroadcastAsUser(lockIntent, UserHandle.SYSTEM, |
| 1010 | Manifest.permission.TRUST_LISTENER, /* options */ null); |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1011 | } |
Rubin Xu | e078133 | 2016-08-19 11:20:34 +0100 | [diff] [blame] | 1012 | } finally { |
| 1013 | Binder.restoreCallingIdentity(identity); |
Rubin Xu | 83a15bc | 2016-08-10 10:53:24 +0100 | [diff] [blame] | 1014 | } |
Clara Bayarri | 56878a9 | 2015-10-29 15:43:55 +0000 | [diff] [blame] | 1015 | } |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1016 | |
| 1017 | @Override |
| 1018 | public boolean isTrustUsuallyManaged(int userId) { |
| 1019 | mContext.enforceCallingPermission(Manifest.permission.TRUST_LISTENER, |
| 1020 | "query trust state"); |
| 1021 | return isTrustUsuallyManagedInternal(userId); |
| 1022 | } |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1023 | |
| 1024 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1025 | public void unlockedByBiometricForUser(int userId, BiometricSourceType biometricSource) { |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1026 | enforceReportPermission(); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1027 | synchronized(mUsersUnlockedByBiometric) { |
| 1028 | mUsersUnlockedByBiometric.put(userId, true); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1029 | } |
| 1030 | mHandler.obtainMessage(MSG_REFRESH_DEVICE_LOCKED_FOR_USER, userId, |
| 1031 | 0 /* arg2 */).sendToTarget(); |
| 1032 | } |
| 1033 | |
| 1034 | @Override |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1035 | public void clearAllBiometricRecognized(BiometricSourceType biometricSource) { |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1036 | enforceReportPermission(); |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1037 | synchronized(mUsersUnlockedByBiometric) { |
| 1038 | mUsersUnlockedByBiometric.clear(); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1039 | } |
| 1040 | mHandler.obtainMessage(MSG_REFRESH_DEVICE_LOCKED_FOR_USER, UserHandle.USER_ALL, |
| 1041 | 0 /* arg2 */).sendToTarget(); |
| 1042 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1043 | }; |
| 1044 | |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1045 | private boolean isTrustUsuallyManagedInternal(int userId) { |
| 1046 | synchronized (mTrustUsuallyManagedForUser) { |
| 1047 | int i = mTrustUsuallyManagedForUser.indexOfKey(userId); |
| 1048 | if (i >= 0) { |
| 1049 | return mTrustUsuallyManagedForUser.valueAt(i); |
| 1050 | } |
| 1051 | } |
| 1052 | // It's not in memory yet, get the value from persisted storage instead |
| 1053 | boolean persistedValue = mLockPatternUtils.isTrustUsuallyManaged(userId); |
| 1054 | synchronized (mTrustUsuallyManagedForUser) { |
| 1055 | int i = mTrustUsuallyManagedForUser.indexOfKey(userId); |
| 1056 | if (i >= 0) { |
| 1057 | // Someone set the trust usually managed in the mean time. Better use that. |
| 1058 | return mTrustUsuallyManagedForUser.valueAt(i); |
| 1059 | } else { |
| 1060 | // .. otherwise it's safe to cache the fetched value now. |
| 1061 | mTrustUsuallyManagedForUser.put(userId, persistedValue); |
| 1062 | return persistedValue; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 1067 | private int resolveProfileParent(int userId) { |
| 1068 | long identity = Binder.clearCallingIdentity(); |
| 1069 | try { |
| 1070 | UserInfo parent = mUserManager.getProfileParent(userId); |
| 1071 | if (parent != null) { |
| 1072 | return parent.getUserHandle().getIdentifier(); |
| 1073 | } |
| 1074 | return userId; |
| 1075 | } finally { |
| 1076 | Binder.restoreCallingIdentity(identity); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1080 | private final Handler mHandler = new Handler() { |
| 1081 | @Override |
| 1082 | public void handleMessage(Message msg) { |
| 1083 | switch (msg.what) { |
| 1084 | case MSG_REGISTER_LISTENER: |
| 1085 | addListener((ITrustListener) msg.obj); |
| 1086 | break; |
| 1087 | case MSG_UNREGISTER_LISTENER: |
| 1088 | removeListener((ITrustListener) msg.obj); |
| 1089 | break; |
| 1090 | case MSG_DISPATCH_UNLOCK_ATTEMPT: |
| 1091 | dispatchUnlockAttempt(msg.arg1 != 0, msg.arg2); |
| 1092 | break; |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 1093 | case MSG_DISPATCH_UNLOCK_LOCKOUT: |
| 1094 | dispatchUnlockLockout(msg.arg1, msg.arg2); |
| 1095 | break; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1096 | case MSG_ENABLED_AGENTS_CHANGED: |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1097 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1098 | // This is also called when the security mode of a user changes. |
| 1099 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1100 | break; |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1101 | case MSG_KEYGUARD_SHOWING_CHANGED: |
Adrian Roos | 7e2e40e | 2014-11-21 15:27:12 +0100 | [diff] [blame] | 1102 | refreshDeviceLockedForUser(mCurrentUser); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1103 | break; |
| 1104 | case MSG_START_USER: |
| 1105 | case MSG_CLEANUP_USER: |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 1106 | case MSG_UNLOCK_USER: |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1107 | refreshAgentList(msg.arg1); |
| 1108 | break; |
| 1109 | case MSG_SWITCH_USER: |
| 1110 | mCurrentUser = msg.arg1; |
| 1111 | refreshDeviceLockedForUser(UserHandle.USER_ALL); |
| 1112 | break; |
Andrew Scull | 85a63bc | 2016-10-24 13:47:47 +0100 | [diff] [blame] | 1113 | case MSG_STOP_USER: |
| 1114 | setDeviceLockedForUser(msg.arg1, true); |
| 1115 | break; |
Adrian Roos | c13723f | 2016-01-12 20:29:03 +0100 | [diff] [blame] | 1116 | case MSG_FLUSH_TRUST_USUALLY_MANAGED: |
| 1117 | SparseBooleanArray usuallyManaged; |
| 1118 | synchronized (mTrustUsuallyManagedForUser) { |
| 1119 | usuallyManaged = mTrustUsuallyManagedForUser.clone(); |
| 1120 | } |
| 1121 | |
| 1122 | for (int i = 0; i < usuallyManaged.size(); i++) { |
| 1123 | int userId = usuallyManaged.keyAt(i); |
| 1124 | boolean value = usuallyManaged.valueAt(i); |
| 1125 | if (value != mLockPatternUtils.isTrustUsuallyManaged(userId)) { |
| 1126 | mLockPatternUtils.setTrustUsuallyManaged(value, userId); |
| 1127 | } |
| 1128 | } |
Adrian Roos | 68771eb | 2016-05-03 14:02:36 -0700 | [diff] [blame] | 1129 | break; |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1130 | case MSG_REFRESH_DEVICE_LOCKED_FOR_USER: |
| 1131 | refreshDeviceLockedForUser(msg.arg1); |
| 1132 | break; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | }; |
| 1136 | |
| 1137 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 1138 | @Override |
| 1139 | public void onSomePackagesChanged() { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1140 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | @Override |
| 1144 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 1145 | // We're interested in all changes, even if just some components get enabled / disabled. |
| 1146 | return true; |
| 1147 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 1148 | |
| 1149 | @Override |
| 1150 | public void onPackageDisappeared(String packageName, int reason) { |
| 1151 | removeAgentsOfPackage(packageName); |
| 1152 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1153 | }; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1154 | |
Lingjun Li | 20914d7 | 2016-11-14 15:39:13 -0800 | [diff] [blame] | 1155 | private static class SettingsAttrs { |
| 1156 | public ComponentName componentName; |
| 1157 | public boolean canUnlockProfile; |
| 1158 | |
| 1159 | public SettingsAttrs( |
| 1160 | ComponentName componentName, |
| 1161 | boolean canUnlockProfile) { |
| 1162 | this.componentName = componentName; |
| 1163 | this.canUnlockProfile = canUnlockProfile; |
| 1164 | } |
| 1165 | }; |
| 1166 | |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1167 | private class Receiver extends BroadcastReceiver { |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1168 | |
| 1169 | @Override |
| 1170 | public void onReceive(Context context, Intent intent) { |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1171 | String action = intent.getAction(); |
| 1172 | if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(action)) { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 1173 | refreshAgentList(getSendingUserId()); |
| 1174 | updateDevicePolicyFeatures(); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1175 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1176 | int userId = getUserId(intent); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1177 | if (userId > 0) { |
| 1178 | maybeEnableFactoryTrustAgents(mLockPatternUtils, userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1179 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1180 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 1181 | int userId = getUserId(intent); |
| 1182 | if (userId > 0) { |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1183 | synchronized (mUserIsTrusted) { |
| 1184 | mUserIsTrusted.delete(userId); |
| 1185 | } |
| 1186 | synchronized (mDeviceLockedForUser) { |
| 1187 | mDeviceLockedForUser.delete(userId); |
| 1188 | } |
Adrian Roos | ae02582 | 2016-11-03 14:46:13 -0700 | [diff] [blame] | 1189 | synchronized (mTrustUsuallyManagedForUser) { |
| 1190 | mTrustUsuallyManagedForUser.delete(userId); |
| 1191 | } |
Gilad Bretter | cb51b8b | 2018-03-22 17:04:51 +0200 | [diff] [blame] | 1192 | synchronized (mUsersUnlockedByBiometric) { |
| 1193 | mUsersUnlockedByBiometric.delete(userId); |
Kevin Chyn | 3fdbbf8 | 2017-05-06 15:11:53 -0700 | [diff] [blame] | 1194 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1195 | refreshAgentList(userId); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 1196 | refreshDeviceLockedForUser(userId); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | private int getUserId(Intent intent) { |
| 1202 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -100); |
| 1203 | if (userId > 0) { |
| 1204 | return userId; |
| 1205 | } else { |
| 1206 | Slog.wtf(TAG, "EXTRA_USER_HANDLE missing or invalid, value=" + userId); |
| 1207 | return -100; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | public void register(Context context) { |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1212 | IntentFilter filter = new IntentFilter(); |
| 1213 | filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 1214 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame] | 1215 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1216 | context.registerReceiverAsUser(this, |
| 1217 | UserHandle.ALL, |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 1218 | filter, |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 1219 | null /* permission */, |
| 1220 | null /* scheduler */); |
| 1221 | } |
| 1222 | } |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 1223 | |
| 1224 | private class StrongAuthTracker extends LockPatternUtils.StrongAuthTracker { |
| 1225 | |
| 1226 | SparseBooleanArray mStartFromSuccessfulUnlock = new SparseBooleanArray(); |
| 1227 | |
| 1228 | public StrongAuthTracker(Context context) { |
| 1229 | super(context); |
| 1230 | } |
| 1231 | |
| 1232 | @Override |
| 1233 | public void onStrongAuthRequiredChanged(int userId) { |
| 1234 | mStartFromSuccessfulUnlock.delete(userId); |
| 1235 | |
| 1236 | if (DEBUG) { |
| 1237 | Log.i(TAG, "onStrongAuthRequiredChanged(" + userId + ") ->" |
| 1238 | + " trustAllowed=" + isTrustAllowedForUser(userId) |
| 1239 | + " agentsCanRun=" + canAgentsRunForUser(userId)); |
| 1240 | } |
| 1241 | |
| 1242 | refreshAgentList(userId); |
| 1243 | |
| 1244 | // The list of active trust agents may not have changed, if there was a previous call |
| 1245 | // to allowTrustFromUnlock, so we update the trust here too. |
| 1246 | updateTrust(userId, 0 /* flags */); |
| 1247 | } |
| 1248 | |
| 1249 | boolean canAgentsRunForUser(int userId) { |
| 1250 | return mStartFromSuccessfulUnlock.get(userId) |
| 1251 | || super.isTrustAllowedForUser(userId); |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * Temporarily suppress strong auth requirements for {@param userId} until strong auth |
| 1256 | * changes again. Must only be called when we know about a successful unlock already |
| 1257 | * before the underlying StrongAuthTracker. |
| 1258 | * |
| 1259 | * Note that this only changes whether trust agents can be started, not the actual trusted |
| 1260 | * value. |
| 1261 | */ |
| 1262 | void allowTrustFromUnlock(int userId) { |
| 1263 | if (userId < UserHandle.USER_SYSTEM) { |
| 1264 | throw new IllegalArgumentException("userId must be a valid user: " + userId); |
| 1265 | } |
| 1266 | boolean previous = canAgentsRunForUser(userId); |
| 1267 | mStartFromSuccessfulUnlock.put(userId, true); |
| 1268 | |
| 1269 | if (DEBUG) { |
| 1270 | Log.i(TAG, "allowTrustFromUnlock(" + userId + ") ->" |
| 1271 | + " trustAllowed=" + isTrustAllowedForUser(userId) |
| 1272 | + " agentsCanRun=" + canAgentsRunForUser(userId)); |
| 1273 | } |
| 1274 | |
| 1275 | if (canAgentsRunForUser(userId) != previous) { |
| 1276 | refreshAgentList(userId); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 1280 | } |