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 | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 19 | import com.android.internal.annotations.GuardedBy; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 20 | import com.android.internal.content.PackageMonitor; |
| 21 | import com.android.internal.widget.LockPatternUtils; |
| 22 | import com.android.server.SystemService; |
| 23 | |
| 24 | import org.xmlpull.v1.XmlPullParser; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | |
| 27 | import android.Manifest; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 29 | import android.app.ActivityManagerNative; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 31 | import android.app.trust.ITrustListener; |
| 32 | import android.app.trust.ITrustManager; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 37 | import android.content.IntentFilter; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 38 | import android.content.pm.ApplicationInfo; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.pm.UserInfo; |
| 42 | import android.content.res.Resources; |
| 43 | import android.content.res.TypedArray; |
| 44 | import android.content.res.XmlResourceParser; |
| 45 | import android.graphics.drawable.Drawable; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 46 | import android.os.Binder; |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 47 | import android.os.DeadObjectException; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 48 | import android.os.Handler; |
| 49 | import android.os.IBinder; |
| 50 | import android.os.Message; |
| 51 | import android.os.RemoteException; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 52 | import android.os.SystemClock; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 53 | import android.os.UserHandle; |
| 54 | import android.os.UserManager; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 55 | import android.provider.Settings; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 56 | import android.service.trust.TrustAgentService; |
| 57 | import android.util.ArraySet; |
| 58 | import android.util.AttributeSet; |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 59 | import android.util.Log; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 60 | import android.util.Slog; |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 61 | import android.util.SparseBooleanArray; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 62 | import android.util.Xml; |
| 63 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 65 | import java.io.IOException; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 66 | import java.io.PrintWriter; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 67 | import java.util.ArrayList; |
| 68 | import java.util.List; |
| 69 | |
| 70 | /** |
| 71 | * Manages trust agents and trust listeners. |
| 72 | * |
| 73 | * It is responsible for binding to the enabled {@link android.service.trust.TrustAgentService}s |
| 74 | * of each user and notifies them about events that are relevant to them. |
| 75 | * It start and stops them based on the value of |
| 76 | * {@link com.android.internal.widget.LockPatternUtils#getEnabledTrustAgents(int)}. |
| 77 | * |
| 78 | * It also keeps a set of {@link android.app.trust.ITrustListener}s that are notified whenever the |
| 79 | * trust state changes for any user. |
| 80 | * |
| 81 | * Trust state and the setting of enabled agents is kept per user and each user has its own |
| 82 | * instance of a {@link android.service.trust.TrustAgentService}. |
| 83 | */ |
| 84 | public class TrustManagerService extends SystemService { |
| 85 | |
| 86 | private static final boolean DEBUG = false; |
| 87 | private static final String TAG = "TrustManagerService"; |
| 88 | |
| 89 | private static final Intent TRUST_AGENT_INTENT = |
| 90 | new Intent(TrustAgentService.SERVICE_INTERFACE); |
Adrian Roos | 18ea893 | 2014-05-28 14:53:06 +0200 | [diff] [blame] | 91 | private static final String PERMISSION_PROVIDE_AGENT = Manifest.permission.PROVIDE_TRUST_AGENT; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 92 | |
| 93 | private static final int MSG_REGISTER_LISTENER = 1; |
| 94 | private static final int MSG_UNREGISTER_LISTENER = 2; |
| 95 | private static final int MSG_DISPATCH_UNLOCK_ATTEMPT = 3; |
| 96 | private static final int MSG_ENABLED_AGENTS_CHANGED = 4; |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 97 | private static final int MSG_REQUIRE_CREDENTIAL_ENTRY = 5; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 98 | |
| 99 | private final ArraySet<AgentInfo> mActiveAgents = new ArraySet<AgentInfo>(); |
| 100 | private final ArrayList<ITrustListener> mTrustListeners = new ArrayList<ITrustListener>(); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 101 | private final Receiver mReceiver = new Receiver(); |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 102 | private final SparseBooleanArray mUserHasAuthenticatedSinceBoot = new SparseBooleanArray(); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 103 | /* package */ final TrustArchive mArchive = new TrustArchive(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 104 | private final Context mContext; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 105 | private final LockPatternUtils mLockPatternUtils; |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 106 | private final UserManager mUserManager; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 107 | private final ActivityManager mActivityManager; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 108 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 109 | @GuardedBy("mUserIsTrusted") |
| 110 | private final SparseBooleanArray mUserIsTrusted = new SparseBooleanArray(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 111 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 112 | private boolean mTrustAgentsCanRun = false; |
| 113 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 114 | public TrustManagerService(Context context) { |
| 115 | super(context); |
| 116 | mContext = context; |
| 117 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 118 | mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 119 | mLockPatternUtils = new LockPatternUtils(context); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onStart() { |
| 124 | publishBinderService(Context.TRUST_SERVICE, mService); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onBootPhase(int phase) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 129 | if (isSafeMode()) { |
| 130 | // No trust agents in safe mode. |
| 131 | return; |
| 132 | } |
| 133 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 134 | mPackageMonitor.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 135 | mReceiver.register(mContext); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 136 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 137 | mTrustAgentsCanRun = true; |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 138 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 139 | } else if (phase == SystemService.PHASE_BOOT_COMPLETED) { |
Adrian Roos | 7b4a38b | 2014-10-21 17:07:39 +0200 | [diff] [blame] | 140 | maybeEnableFactoryTrustAgents(mLockPatternUtils, UserHandle.USER_OWNER); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // Agent management |
| 145 | |
| 146 | private static final class AgentInfo { |
| 147 | CharSequence label; |
| 148 | Drawable icon; |
| 149 | ComponentName component; // service that implements ITrustAgent |
| 150 | ComponentName settings; // setting to launch to modify agent. |
| 151 | TrustAgentWrapper agent; |
| 152 | int userId; |
| 153 | |
| 154 | @Override |
| 155 | public boolean equals(Object other) { |
| 156 | if (!(other instanceof AgentInfo)) { |
| 157 | return false; |
| 158 | } |
| 159 | AgentInfo o = (AgentInfo) other; |
| 160 | return component.equals(o.component) && userId == o.userId; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int hashCode() { |
| 165 | return component.hashCode() * 31 + userId; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private void updateTrustAll() { |
| 170 | List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 171 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 172 | updateTrust(userInfo.id, false); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 176 | public void updateTrust(int userId, boolean initiatedByUser) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 177 | dispatchOnTrustManagedChanged(aggregateIsTrustManaged(userId), userId); |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 178 | boolean trusted = aggregateIsTrusted(userId); |
| 179 | synchronized (mUserIsTrusted) { |
| 180 | mUserIsTrusted.put(userId, trusted); |
| 181 | } |
| 182 | dispatchOnTrustChanged(trusted, userId, initiatedByUser); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 185 | void refreshAgentList(int userId) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 186 | if (DEBUG) Slog.d(TAG, "refreshAgentList()"); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 187 | if (!mTrustAgentsCanRun) { |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 188 | return; |
| 189 | } |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 190 | if (userId != UserHandle.USER_ALL && userId < UserHandle.USER_OWNER) { |
| 191 | Log.e(TAG, "refreshAgentList(userId=" + userId + "): Invalid user handle," |
| 192 | + " must be USER_ALL or a specific user.", new Throwable("here")); |
| 193 | userId = UserHandle.USER_ALL; |
| 194 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 195 | PackageManager pm = mContext.getPackageManager(); |
| 196 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 197 | List<UserInfo> userInfos; |
| 198 | if (userId == UserHandle.USER_ALL) { |
| 199 | userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 200 | } else { |
| 201 | userInfos = new ArrayList<>(); |
| 202 | userInfos.add(mUserManager.getUserInfo(userId)); |
| 203 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 204 | LockPatternUtils lockPatternUtils = mLockPatternUtils; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 205 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 206 | ArraySet<AgentInfo> obsoleteAgents = new ArraySet<>(); |
| 207 | obsoleteAgents.addAll(mActiveAgents); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 208 | |
| 209 | for (UserInfo userInfo : userInfos) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 210 | if (userInfo.partial || !userInfo.isEnabled() || userInfo.guestToRemove) continue; |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 211 | if (!userInfo.supportsSwitchTo()) continue; |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 212 | if (!mActivityManager.isUserRunning(userInfo.id)) continue; |
Adrian Roos | 1572ee3 | 2014-09-01 16:24:32 +0200 | [diff] [blame] | 213 | if (lockPatternUtils.getKeyguardStoredPasswordQuality(userInfo.id) |
Adrian Roos | 4b9e324 | 2014-08-20 23:36:25 +0200 | [diff] [blame] | 214 | == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) continue; |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 215 | if (!mUserHasAuthenticatedSinceBoot.get(userInfo.id)) continue; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 216 | DevicePolicyManager dpm = lockPatternUtils.getDevicePolicyManager(); |
| 217 | int disabledFeatures = dpm.getKeyguardDisabledFeatures(null, userInfo.id); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 218 | final boolean disableTrustAgents = |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 219 | (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0; |
| 220 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 221 | List<ComponentName> enabledAgents = lockPatternUtils.getEnabledTrustAgents(userInfo.id); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 222 | if (enabledAgents == null) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 223 | continue; |
| 224 | } |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 225 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userInfo.id); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 226 | for (ResolveInfo resolveInfo : resolveInfos) { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 227 | ComponentName name = getComponentName(resolveInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 228 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 229 | if (!enabledAgents.contains(name)) continue; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 230 | if (disableTrustAgents) { |
| 231 | List<String> features = |
| 232 | dpm.getTrustAgentFeaturesEnabled(null /* admin */, name); |
| 233 | // Disable agent if no features are enabled. |
| 234 | if (features == null || features.isEmpty()) continue; |
| 235 | } |
| 236 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 237 | AgentInfo agentInfo = new AgentInfo(); |
| 238 | agentInfo.component = name; |
| 239 | agentInfo.userId = userInfo.id; |
| 240 | if (!mActiveAgents.contains(agentInfo)) { |
| 241 | agentInfo.label = resolveInfo.loadLabel(pm); |
| 242 | agentInfo.icon = resolveInfo.loadIcon(pm); |
| 243 | agentInfo.settings = getSettingsComponentName(pm, resolveInfo); |
| 244 | agentInfo.agent = new TrustAgentWrapper(mContext, this, |
| 245 | new Intent().setComponent(name), userInfo.getUserHandle()); |
| 246 | mActiveAgents.add(agentInfo); |
| 247 | } else { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 248 | obsoleteAgents.remove(agentInfo); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | boolean trustMayHaveChanged = false; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 254 | for (int i = 0; i < obsoleteAgents.size(); i++) { |
| 255 | AgentInfo info = obsoleteAgents.valueAt(i); |
Adrian Roos | e681c27 | 2014-09-08 14:03:47 +0200 | [diff] [blame] | 256 | if (userId == UserHandle.USER_ALL || userId == info.userId) { |
| 257 | if (info.agent.isManagingTrust()) { |
| 258 | trustMayHaveChanged = true; |
| 259 | } |
| 260 | info.agent.unbind(); |
| 261 | mActiveAgents.remove(info); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 262 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (trustMayHaveChanged) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 266 | if (userId == UserHandle.USER_ALL) { |
| 267 | updateTrustAll(); |
| 268 | } else { |
| 269 | updateTrust(userId, false /* initiatedByUser */); |
| 270 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 274 | void updateDevicePolicyFeatures() { |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 275 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 276 | AgentInfo info = mActiveAgents.valueAt(i); |
| 277 | if (info.agent.isConnected()) { |
| 278 | info.agent.updateDevicePolicyFeatures(); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 283 | private void removeAgentsOfPackage(String packageName) { |
| 284 | boolean trustMayHaveChanged = false; |
| 285 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 286 | AgentInfo info = mActiveAgents.valueAt(i); |
| 287 | if (packageName.equals(info.component.getPackageName())) { |
| 288 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 289 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 290 | trustMayHaveChanged = true; |
| 291 | } |
| 292 | info.agent.unbind(); |
| 293 | mActiveAgents.removeAt(i); |
| 294 | } |
| 295 | } |
| 296 | if (trustMayHaveChanged) { |
| 297 | updateTrustAll(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | public void resetAgent(ComponentName name, int userId) { |
| 302 | boolean trustMayHaveChanged = false; |
| 303 | for (int i = mActiveAgents.size() - 1; i >= 0; i--) { |
| 304 | AgentInfo info = mActiveAgents.valueAt(i); |
| 305 | if (name.equals(info.component) && userId == info.userId) { |
| 306 | Log.i(TAG, "Resetting agent " + info.component.flattenToShortString()); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 307 | if (info.agent.isManagingTrust()) { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 308 | trustMayHaveChanged = true; |
| 309 | } |
| 310 | info.agent.unbind(); |
| 311 | mActiveAgents.removeAt(i); |
| 312 | } |
| 313 | } |
| 314 | if (trustMayHaveChanged) { |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 315 | updateTrust(userId, false); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 316 | } |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 317 | refreshAgentList(userId); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 320 | private ComponentName getSettingsComponentName(PackageManager pm, ResolveInfo resolveInfo) { |
| 321 | if (resolveInfo == null || resolveInfo.serviceInfo == null |
| 322 | || resolveInfo.serviceInfo.metaData == null) return null; |
| 323 | String cn = null; |
| 324 | XmlResourceParser parser = null; |
| 325 | Exception caughtException = null; |
| 326 | try { |
| 327 | parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, |
| 328 | TrustAgentService.TRUST_AGENT_META_DATA); |
| 329 | if (parser == null) { |
| 330 | Slog.w(TAG, "Can't find " + TrustAgentService.TRUST_AGENT_META_DATA + " meta-data"); |
| 331 | return null; |
| 332 | } |
| 333 | Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo); |
| 334 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 335 | int type; |
| 336 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 337 | && type != XmlPullParser.START_TAG) { |
| 338 | // Drain preamble. |
| 339 | } |
| 340 | String nodeName = parser.getName(); |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 341 | if (!"trust-agent".equals(nodeName)) { |
| 342 | Slog.w(TAG, "Meta-data does not start with trust-agent tag"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 343 | return null; |
| 344 | } |
| 345 | TypedArray sa = res |
| 346 | .obtainAttributes(attrs, com.android.internal.R.styleable.TrustAgent); |
| 347 | cn = sa.getString(com.android.internal.R.styleable.TrustAgent_settingsActivity); |
| 348 | sa.recycle(); |
| 349 | } catch (PackageManager.NameNotFoundException e) { |
| 350 | caughtException = e; |
| 351 | } catch (IOException e) { |
| 352 | caughtException = e; |
| 353 | } catch (XmlPullParserException e) { |
| 354 | caughtException = e; |
| 355 | } finally { |
| 356 | if (parser != null) parser.close(); |
| 357 | } |
| 358 | if (caughtException != null) { |
| 359 | Slog.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException); |
| 360 | return null; |
| 361 | } |
| 362 | if (cn == null) { |
| 363 | return null; |
| 364 | } |
| 365 | if (cn.indexOf('/') < 0) { |
| 366 | cn = resolveInfo.serviceInfo.packageName + "/" + cn; |
| 367 | } |
| 368 | return ComponentName.unflattenFromString(cn); |
| 369 | } |
| 370 | |
| 371 | private ComponentName getComponentName(ResolveInfo resolveInfo) { |
| 372 | if (resolveInfo == null || resolveInfo.serviceInfo == null) return null; |
| 373 | return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); |
| 374 | } |
| 375 | |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 376 | private void maybeEnableFactoryTrustAgents(LockPatternUtils utils, int userId) { |
| 377 | if (0 != Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 378 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 0, userId)) { |
| 379 | return; |
| 380 | } |
| 381 | PackageManager pm = mContext.getPackageManager(); |
| 382 | List<ResolveInfo> resolveInfos = resolveAllowedTrustAgents(pm, userId); |
| 383 | ArraySet<ComponentName> discoveredAgents = new ArraySet<>(); |
| 384 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 385 | ComponentName componentName = getComponentName(resolveInfo); |
| 386 | int applicationInfoFlags = resolveInfo.serviceInfo.applicationInfo.flags; |
| 387 | if ((applicationInfoFlags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 388 | Log.i(TAG, "Leaving agent " + componentName + " disabled because package " |
| 389 | + "is not a system package."); |
| 390 | continue; |
| 391 | } |
| 392 | discoveredAgents.add(componentName); |
| 393 | } |
| 394 | |
| 395 | List<ComponentName> previouslyEnabledAgents = utils.getEnabledTrustAgents(userId); |
| 396 | if (previouslyEnabledAgents != null) { |
| 397 | discoveredAgents.addAll(previouslyEnabledAgents); |
| 398 | } |
| 399 | utils.setEnabledTrustAgents(discoveredAgents, userId); |
| 400 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 401 | Settings.Secure.TRUST_AGENTS_INITIALIZED, 1, userId); |
| 402 | } |
| 403 | |
| 404 | private List<ResolveInfo> resolveAllowedTrustAgents(PackageManager pm, int userId) { |
| 405 | List<ResolveInfo> resolveInfos = pm.queryIntentServicesAsUser(TRUST_AGENT_INTENT, |
| 406 | 0 /* flags */, userId); |
| 407 | ArrayList<ResolveInfo> allowedAgents = new ArrayList<>(resolveInfos.size()); |
| 408 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 409 | if (resolveInfo.serviceInfo == null) continue; |
| 410 | if (resolveInfo.serviceInfo.applicationInfo == null) continue; |
| 411 | String packageName = resolveInfo.serviceInfo.packageName; |
| 412 | if (pm.checkPermission(PERMISSION_PROVIDE_AGENT, packageName) |
| 413 | != PackageManager.PERMISSION_GRANTED) { |
| 414 | ComponentName name = getComponentName(resolveInfo); |
| 415 | Log.w(TAG, "Skipping agent " + name + " because package does not have" |
| 416 | + " permission " + PERMISSION_PROVIDE_AGENT + "."); |
| 417 | continue; |
| 418 | } |
| 419 | allowedAgents.add(resolveInfo); |
| 420 | } |
| 421 | return allowedAgents; |
| 422 | } |
| 423 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 424 | // Agent dispatch and aggregation |
| 425 | |
| 426 | private boolean aggregateIsTrusted(int userId) { |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 427 | if (!mUserHasAuthenticatedSinceBoot.get(userId)) { |
| 428 | return false; |
| 429 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 430 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 431 | AgentInfo info = mActiveAgents.valueAt(i); |
| 432 | if (info.userId == userId) { |
| 433 | if (info.agent.isTrusted()) { |
| 434 | return true; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | return false; |
| 439 | } |
| 440 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 441 | private boolean aggregateIsTrustManaged(int userId) { |
| 442 | if (!mUserHasAuthenticatedSinceBoot.get(userId)) { |
| 443 | return false; |
| 444 | } |
| 445 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 446 | AgentInfo info = mActiveAgents.valueAt(i); |
| 447 | if (info.userId == userId) { |
| 448 | if (info.agent.isManagingTrust()) { |
| 449 | return true; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 456 | private void dispatchUnlockAttempt(boolean successful, int userId) { |
| 457 | for (int i = 0; i < mActiveAgents.size(); i++) { |
| 458 | AgentInfo info = mActiveAgents.valueAt(i); |
| 459 | if (info.userId == userId) { |
| 460 | info.agent.onUnlockAttempt(successful); |
| 461 | } |
| 462 | } |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 463 | |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 464 | if (successful) { |
| 465 | updateUserHasAuthenticated(userId); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | private void updateUserHasAuthenticated(int userId) { |
| 470 | if (!mUserHasAuthenticatedSinceBoot.get(userId)) { |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 471 | mUserHasAuthenticatedSinceBoot.put(userId, true); |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 472 | refreshAgentList(userId); |
Adrian Roos | 7046bfd | 2014-05-16 21:20:54 +0200 | [diff] [blame] | 473 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 476 | |
| 477 | private void requireCredentialEntry(int userId) { |
| 478 | if (userId == UserHandle.USER_ALL) { |
| 479 | mUserHasAuthenticatedSinceBoot.clear(); |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 480 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 481 | } else { |
| 482 | mUserHasAuthenticatedSinceBoot.put(userId, false); |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 483 | refreshAgentList(userId); |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 487 | // Listeners |
| 488 | |
| 489 | private void addListener(ITrustListener listener) { |
| 490 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 491 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
| 492 | return; |
| 493 | } |
| 494 | } |
| 495 | mTrustListeners.add(listener); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 496 | updateTrustAll(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | private void removeListener(ITrustListener listener) { |
| 500 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 501 | if (mTrustListeners.get(i).asBinder() == listener.asBinder()) { |
Jay Civelli | 979a32e | 2014-07-18 16:47:36 -0700 | [diff] [blame] | 502 | mTrustListeners.remove(i); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 508 | private void dispatchOnTrustChanged(boolean enabled, int userId, boolean initiatedByUser) { |
| 509 | if (!enabled) initiatedByUser = false; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 510 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 511 | try { |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 512 | mTrustListeners.get(i).onTrustChanged(enabled, userId, initiatedByUser); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 513 | } catch (DeadObjectException e) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 514 | Slog.d(TAG, "Removing dead TrustListener."); |
| 515 | mTrustListeners.remove(i); |
| 516 | i--; |
| 517 | } catch (RemoteException e) { |
| 518 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private void dispatchOnTrustManagedChanged(boolean managed, int userId) { |
| 524 | for (int i = 0; i < mTrustListeners.size(); i++) { |
| 525 | try { |
| 526 | mTrustListeners.get(i).onTrustManagedChanged(managed, userId); |
| 527 | } catch (DeadObjectException e) { |
| 528 | Slog.d(TAG, "Removing dead TrustListener."); |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 529 | mTrustListeners.remove(i); |
| 530 | i--; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 531 | } catch (RemoteException e) { |
Adrian Roos | a4ba56b | 2014-05-20 12:56:25 +0200 | [diff] [blame] | 532 | Slog.e(TAG, "Exception while notifying TrustListener.", e); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 537 | // User lifecycle |
| 538 | |
| 539 | @Override |
| 540 | public void onStartUser(int userId) { |
| 541 | refreshAgentList(userId); |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void onCleanupUser(int userId) { |
| 546 | refreshAgentList(userId); |
| 547 | } |
| 548 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 549 | // Plumbing |
| 550 | |
| 551 | private final IBinder mService = new ITrustManager.Stub() { |
| 552 | @Override |
| 553 | public void reportUnlockAttempt(boolean authenticated, int userId) throws RemoteException { |
| 554 | enforceReportPermission(); |
| 555 | mHandler.obtainMessage(MSG_DISPATCH_UNLOCK_ATTEMPT, authenticated ? 1 : 0, userId) |
| 556 | .sendToTarget(); |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void reportEnabledTrustAgentsChanged(int userId) throws RemoteException { |
| 561 | enforceReportPermission(); |
| 562 | // coalesce refresh messages. |
| 563 | mHandler.removeMessages(MSG_ENABLED_AGENTS_CHANGED); |
| 564 | mHandler.sendEmptyMessage(MSG_ENABLED_AGENTS_CHANGED); |
| 565 | } |
| 566 | |
| 567 | @Override |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 568 | public void reportRequireCredentialEntry(int userId) throws RemoteException { |
| 569 | enforceReportPermission(); |
| 570 | if (userId == UserHandle.USER_ALL || userId >= UserHandle.USER_OWNER) { |
| 571 | mHandler.obtainMessage(MSG_REQUIRE_CREDENTIAL_ENTRY, userId, 0).sendToTarget(); |
| 572 | } else { |
| 573 | throw new IllegalArgumentException( |
| 574 | "userId must be an explicit user id or USER_ALL"); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 579 | public void registerTrustListener(ITrustListener trustListener) throws RemoteException { |
| 580 | enforceListenerPermission(); |
| 581 | mHandler.obtainMessage(MSG_REGISTER_LISTENER, trustListener).sendToTarget(); |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | public void unregisterTrustListener(ITrustListener trustListener) throws RemoteException { |
| 586 | enforceListenerPermission(); |
| 587 | mHandler.obtainMessage(MSG_UNREGISTER_LISTENER, trustListener).sendToTarget(); |
| 588 | } |
| 589 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 590 | @Override |
| 591 | public boolean isTrusted(int userId) throws RemoteException { |
| 592 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
| 593 | false /* allowAll */, true /* requireFull */, "isTrusted", null); |
| 594 | userId = resolveProfileParent(userId); |
| 595 | synchronized (mUserIsTrusted) { |
| 596 | return mUserIsTrusted.get(userId); |
| 597 | } |
| 598 | } |
| 599 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 600 | private void enforceReportPermission() { |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 601 | mContext.enforceCallingOrSelfPermission( |
| 602 | Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE, "reporting trust events"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | private void enforceListenerPermission() { |
| 606 | mContext.enforceCallingPermission(Manifest.permission.TRUST_LISTENER, |
| 607 | "register trust listener"); |
| 608 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 609 | |
| 610 | @Override |
| 611 | protected void dump(FileDescriptor fd, final PrintWriter fout, String[] args) { |
| 612 | mContext.enforceCallingPermission(Manifest.permission.DUMP, |
| 613 | "dumping TrustManagerService"); |
Adrian Roos | 49d5345 | 2014-10-24 15:48:39 +0200 | [diff] [blame] | 614 | if (isSafeMode()) { |
| 615 | fout.println("disabled because the system is in safe mode."); |
| 616 | return; |
| 617 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 618 | if (!mTrustAgentsCanRun) { |
| 619 | fout.println("disabled because the third-party apps can't run yet."); |
| 620 | return; |
| 621 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 622 | final UserInfo currentUser; |
| 623 | final List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */); |
| 624 | try { |
| 625 | currentUser = ActivityManagerNative.getDefault().getCurrentUser(); |
| 626 | } catch (RemoteException e) { |
| 627 | throw new RuntimeException(e); |
| 628 | } |
| 629 | mHandler.runWithScissors(new Runnable() { |
| 630 | @Override |
| 631 | public void run() { |
| 632 | fout.println("Trust manager state:"); |
| 633 | for (UserInfo user : userInfos) { |
| 634 | dumpUser(fout, user, user.id == currentUser.id); |
| 635 | } |
| 636 | } |
| 637 | }, 1500); |
| 638 | } |
| 639 | |
| 640 | private void dumpUser(PrintWriter fout, UserInfo user, boolean isCurrent) { |
| 641 | fout.printf(" User \"%s\" (id=%d, flags=%#x)", |
| 642 | user.name, user.id, user.flags); |
| 643 | if (isCurrent) { |
| 644 | fout.print(" (current)"); |
| 645 | } |
| 646 | fout.print(": trusted=" + dumpBool(aggregateIsTrusted(user.id))); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 647 | fout.print(", trustManaged=" + dumpBool(aggregateIsTrustManaged(user.id))); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 648 | fout.println(); |
| 649 | fout.println(" Enabled agents:"); |
| 650 | boolean duplicateSimpleNames = false; |
| 651 | ArraySet<String> simpleNames = new ArraySet<String>(); |
| 652 | for (AgentInfo info : mActiveAgents) { |
| 653 | if (info.userId != user.id) { continue; } |
| 654 | boolean trusted = info.agent.isTrusted(); |
| 655 | fout.print(" "); fout.println(info.component.flattenToShortString()); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 656 | fout.print(" bound=" + dumpBool(info.agent.isBound())); |
| 657 | fout.print(", connected=" + dumpBool(info.agent.isConnected())); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 658 | fout.print(", managingTrust=" + dumpBool(info.agent.isManagingTrust())); |
| 659 | fout.print(", trusted=" + dumpBool(trusted)); |
| 660 | fout.println(); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 661 | if (trusted) { |
| 662 | fout.println(" message=\"" + info.agent.getMessage() + "\""); |
| 663 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 664 | if (!info.agent.isConnected()) { |
| 665 | String restartTime = TrustArchive.formatDuration( |
| 666 | info.agent.getScheduledRestartUptimeMillis() |
| 667 | - SystemClock.uptimeMillis()); |
| 668 | fout.println(" restartScheduledAt=" + restartTime); |
| 669 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 670 | if (!simpleNames.add(TrustArchive.getSimpleName(info.component))) { |
| 671 | duplicateSimpleNames = true; |
| 672 | } |
| 673 | } |
| 674 | fout.println(" Events:"); |
| 675 | mArchive.dump(fout, 50, user.id, " " /* linePrefix */, duplicateSimpleNames); |
| 676 | fout.println(); |
| 677 | } |
| 678 | |
| 679 | private String dumpBool(boolean b) { |
| 680 | return b ? "1" : "0"; |
| 681 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 682 | }; |
| 683 | |
Adrian Roos | bcd0765 | 2014-10-22 16:57:16 +0200 | [diff] [blame] | 684 | private int resolveProfileParent(int userId) { |
| 685 | long identity = Binder.clearCallingIdentity(); |
| 686 | try { |
| 687 | UserInfo parent = mUserManager.getProfileParent(userId); |
| 688 | if (parent != null) { |
| 689 | return parent.getUserHandle().getIdentifier(); |
| 690 | } |
| 691 | return userId; |
| 692 | } finally { |
| 693 | Binder.restoreCallingIdentity(identity); |
| 694 | } |
| 695 | } |
| 696 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 697 | private final Handler mHandler = new Handler() { |
| 698 | @Override |
| 699 | public void handleMessage(Message msg) { |
| 700 | switch (msg.what) { |
| 701 | case MSG_REGISTER_LISTENER: |
| 702 | addListener((ITrustListener) msg.obj); |
| 703 | break; |
| 704 | case MSG_UNREGISTER_LISTENER: |
| 705 | removeListener((ITrustListener) msg.obj); |
| 706 | break; |
| 707 | case MSG_DISPATCH_UNLOCK_ATTEMPT: |
| 708 | dispatchUnlockAttempt(msg.arg1 != 0, msg.arg2); |
| 709 | break; |
| 710 | case MSG_ENABLED_AGENTS_CHANGED: |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 711 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 712 | break; |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 713 | case MSG_REQUIRE_CREDENTIAL_ENTRY: |
| 714 | requireCredentialEntry(msg.arg1); |
| 715 | break; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | }; |
| 719 | |
| 720 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 721 | @Override |
| 722 | public void onSomePackagesChanged() { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 723 | refreshAgentList(UserHandle.USER_ALL); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 728 | // We're interested in all changes, even if just some components get enabled / disabled. |
| 729 | return true; |
| 730 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 731 | |
| 732 | @Override |
| 733 | public void onPackageDisappeared(String packageName, int reason) { |
| 734 | removeAgentsOfPackage(packageName); |
| 735 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 736 | }; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 737 | |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 738 | private class Receiver extends BroadcastReceiver { |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 739 | |
| 740 | @Override |
| 741 | public void onReceive(Context context, Intent intent) { |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 742 | String action = intent.getAction(); |
| 743 | if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(action)) { |
Marco Fucci | 4e68f11 | 2014-08-29 12:31:48 -0700 | [diff] [blame] | 744 | refreshAgentList(getSendingUserId()); |
| 745 | updateDevicePolicyFeatures(); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 746 | } else if (Intent.ACTION_USER_PRESENT.equals(action)) { |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 747 | updateUserHasAuthenticated(getSendingUserId()); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 748 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 749 | int userId = getUserId(intent); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 750 | if (userId > 0) { |
| 751 | maybeEnableFactoryTrustAgents(mLockPatternUtils, userId); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 752 | } |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 753 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 754 | int userId = getUserId(intent); |
| 755 | if (userId > 0) { |
| 756 | mUserHasAuthenticatedSinceBoot.delete(userId); |
| 757 | mUserIsTrusted.delete(userId); |
| 758 | refreshAgentList(userId); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | private int getUserId(Intent intent) { |
| 764 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -100); |
| 765 | if (userId > 0) { |
| 766 | return userId; |
| 767 | } else { |
| 768 | Slog.wtf(TAG, "EXTRA_USER_HANDLE missing or invalid, value=" + userId); |
| 769 | return -100; |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | public void register(Context context) { |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 774 | IntentFilter filter = new IntentFilter(); |
| 775 | filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 776 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Adrian Roos | 3870d45 | 2014-09-05 18:22:28 +0200 | [diff] [blame] | 777 | filter.addAction(Intent.ACTION_USER_ADDED); |
Adrian Roos | cbe614f | 2014-10-28 20:16:12 +0100 | [diff] [blame^] | 778 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 779 | context.registerReceiverAsUser(this, |
| 780 | UserHandle.ALL, |
Adrian Roos | 9dbe190 | 2014-08-13 18:25:52 +0200 | [diff] [blame] | 781 | filter, |
Adrian Roos | ca36b95 | 2014-05-16 18:52:29 +0200 | [diff] [blame] | 782 | null /* permission */, |
| 783 | null /* scheduler */); |
| 784 | } |
| 785 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 786 | } |