Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 20 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame^] | 21 | import com.android.internal.util.JournaledFile; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 22 | import com.android.internal.util.XmlUtils; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 23 | import com.android.internal.widget.LockPatternUtils; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 24 | |
| 25 | import org.xmlpull.v1.XmlPullParser; |
| 26 | import org.xmlpull.v1.XmlPullParserException; |
| 27 | import org.xmlpull.v1.XmlSerializer; |
| 28 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 29 | import android.app.Activity; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 30 | import android.app.admin.DeviceAdminInfo; |
| 31 | import android.app.admin.DeviceAdminReceiver; |
| 32 | import android.app.admin.DevicePolicyManager; |
| 33 | import android.app.admin.IDevicePolicyManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 34 | import android.content.BroadcastReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 35 | import android.content.ComponentName; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.pm.PackageManager; |
| 39 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 41 | import android.os.Binder; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 42 | import android.os.IBinder; |
| 43 | import android.os.IPowerManager; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 44 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 45 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 48 | import android.os.SystemClock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 49 | import android.util.Log; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 50 | import android.util.PrintWriterPrinter; |
| 51 | import android.util.Printer; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 52 | import android.util.Xml; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 53 | import android.view.WindowManagerPolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 54 | |
| 55 | import java.io.File; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 56 | import java.io.FileDescriptor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 57 | import java.io.FileInputStream; |
| 58 | import java.io.FileOutputStream; |
| 59 | import java.io.IOException; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 60 | import java.io.PrintWriter; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 61 | import java.util.ArrayList; |
| 62 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 63 | import java.util.List; |
| 64 | |
| 65 | /** |
| 66 | * Implementation of the device policy APIs. |
| 67 | */ |
| 68 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 69 | static final String TAG = "DevicePolicyManagerService"; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 70 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 71 | final Context mContext; |
| 72 | final MyPackageMonitor mMonitor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 73 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 74 | IPowerManager mIPowerManager; |
| 75 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 76 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 77 | int mActivePasswordLength = 0; |
| 78 | int mFailedPasswordAttempts = 0; |
| 79 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 80 | int mPasswordOwner = -1; |
| 81 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 82 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 83 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 84 | final ArrayList<ActiveAdmin> mAdminList |
| 85 | = new ArrayList<ActiveAdmin>(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 86 | |
| 87 | static class ActiveAdmin { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 88 | final DeviceAdminInfo info; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 89 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 90 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 91 | int minimumPasswordLength = 0; |
| 92 | long maximumTimeToUnlock = 0; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 93 | int maximumFailedPasswordsForWipe = 0; |
| 94 | |
| 95 | ActiveAdmin(DeviceAdminInfo _info) { |
| 96 | info = _info; |
| 97 | } |
| 98 | |
| 99 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
| 100 | |
| 101 | void writeToXml(XmlSerializer out) |
| 102 | throws IllegalArgumentException, IllegalStateException, IOException { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 103 | out.startTag(null, "policies"); |
| 104 | info.writePoliciesToXml(out); |
| 105 | out.endTag(null, "policies"); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 106 | if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 107 | out.startTag(null, "password-quality"); |
| 108 | out.attribute(null, "value", Integer.toString(passwordQuality)); |
| 109 | out.endTag(null, "password-quality"); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 110 | if (minimumPasswordLength > 0) { |
| 111 | out.startTag(null, "min-password-length"); |
| 112 | out.attribute(null, "value", Integer.toString(minimumPasswordLength)); |
| 113 | out.endTag(null, "mn-password-length"); |
| 114 | } |
| 115 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 116 | if (maximumTimeToUnlock != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 117 | out.startTag(null, "max-time-to-unlock"); |
| 118 | out.attribute(null, "value", Long.toString(maximumTimeToUnlock)); |
| 119 | out.endTag(null, "max-time-to-unlock"); |
| 120 | } |
| 121 | if (maximumFailedPasswordsForWipe != 0) { |
| 122 | out.startTag(null, "max-failed-password-wipe"); |
| 123 | out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe)); |
| 124 | out.endTag(null, "max-failed-password-wipe"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void readFromXml(XmlPullParser parser) |
| 129 | throws XmlPullParserException, IOException { |
| 130 | int outerDepth = parser.getDepth(); |
| 131 | int type; |
| 132 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 133 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 134 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 135 | continue; |
| 136 | } |
| 137 | String tag = parser.getName(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 138 | if ("policies".equals(tag)) { |
| 139 | info.readPoliciesFromXml(parser); |
| 140 | } else if ("password-quality".equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 141 | passwordQuality = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 142 | parser.getAttributeValue(null, "value")); |
| 143 | } else if ("min-password-length".equals(tag)) { |
| 144 | minimumPasswordLength = Integer.parseInt( |
| 145 | parser.getAttributeValue(null, "value")); |
| 146 | } else if ("max-time-to-unlock".equals(tag)) { |
| 147 | maximumTimeToUnlock = Long.parseLong( |
| 148 | parser.getAttributeValue(null, "value")); |
| 149 | } else if ("max-failed-password-wipe".equals(tag)) { |
| 150 | maximumFailedPasswordsForWipe = Integer.parseInt( |
| 151 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 152 | } else { |
| 153 | Log.w(TAG, "Unknown admin tag: " + tag); |
| 154 | } |
| 155 | XmlUtils.skipCurrentTag(parser); |
| 156 | } |
| 157 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 158 | |
| 159 | void dump(String prefix, PrintWriter pw) { |
| 160 | pw.print(prefix); pw.print("uid="); pw.println(getUid()); |
| 161 | pw.print(prefix); pw.println("policies:"); |
| 162 | ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies(); |
| 163 | if (pols != null) { |
| 164 | for (int i=0; i<pols.size(); i++) { |
| 165 | pw.print(prefix); pw.print(" "); pw.println(pols.get(i).tag); |
| 166 | } |
| 167 | } |
| 168 | pw.print(prefix); pw.print("passwordQuality="); |
| 169 | pw.print(passwordQuality); |
| 170 | pw.print(" minimumPasswordLength="); |
| 171 | pw.println(minimumPasswordLength); |
| 172 | pw.print(prefix); pw.print("maximumTimeToUnlock="); |
| 173 | pw.println(maximumTimeToUnlock); |
| 174 | pw.print(prefix); pw.print("maximumFailedPasswordsForWipe="); |
| 175 | pw.println(maximumFailedPasswordsForWipe); |
| 176 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | class MyPackageMonitor extends PackageMonitor { |
| 180 | public void onSomePackagesChanged() { |
| 181 | synchronized (DevicePolicyManagerService.this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 182 | boolean removed = false; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 183 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 184 | ActiveAdmin aa = mAdminList.get(i); |
| 185 | int change = isPackageDisappearing(aa.info.getPackageName()); |
| 186 | if (change == PACKAGE_PERMANENT_CHANGE |
| 187 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 188 | Log.w(TAG, "Admin unexpectedly uninstalled: " |
| 189 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 190 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 191 | mAdminList.remove(i); |
| 192 | } else if (isPackageModified(aa.info.getPackageName())) { |
| 193 | try { |
| 194 | mContext.getPackageManager().getReceiverInfo( |
| 195 | aa.info.getComponent(), 0); |
| 196 | } catch (NameNotFoundException e) { |
| 197 | Log.w(TAG, "Admin package change removed component: " |
| 198 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 199 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 200 | mAdminList.remove(i); |
| 201 | } |
| 202 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 203 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 204 | if (removed) { |
| 205 | validatePasswordOwnerLocked(); |
| 206 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Instantiates the service. |
| 213 | */ |
| 214 | public DevicePolicyManagerService(Context context) { |
| 215 | mContext = context; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 216 | mMonitor = new MyPackageMonitor(); |
| 217 | mMonitor.register(context, true); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 220 | private IPowerManager getIPowerManager() { |
| 221 | if (mIPowerManager == null) { |
| 222 | IBinder b = ServiceManager.getService(Context.POWER_SERVICE); |
| 223 | mIPowerManager = IPowerManager.Stub.asInterface(b); |
| 224 | } |
| 225 | return mIPowerManager; |
| 226 | } |
| 227 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 228 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 229 | ActiveAdmin admin = mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 230 | if (admin != null |
| 231 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 232 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 233 | return admin; |
| 234 | } |
| 235 | return null; |
| 236 | } |
| 237 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 238 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 239 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 240 | final int callingUid = Binder.getCallingUid(); |
| 241 | if (who != null) { |
| 242 | ActiveAdmin admin = mAdminMap.get(who); |
| 243 | if (admin == null) { |
| 244 | throw new SecurityException("No active admin " + who); |
| 245 | } |
| 246 | if (admin.getUid() != callingUid) { |
| 247 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 248 | + Binder.getCallingUid()); |
| 249 | } |
| 250 | if (!admin.info.usesPolicy(reqPolicy)) { |
| 251 | throw new SecurityException("Admin " + admin.info.getComponent() |
| 252 | + " did not specify uses-policy for: " |
| 253 | + admin.info.getTagForPolicy(reqPolicy)); |
| 254 | } |
| 255 | return admin; |
| 256 | } else { |
| 257 | final int N = mAdminList.size(); |
| 258 | for (int i=0; i<N; i++) { |
| 259 | ActiveAdmin admin = mAdminList.get(i); |
| 260 | if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) { |
| 261 | return admin; |
| 262 | } |
| 263 | } |
| 264 | throw new SecurityException("No active admin owned by uid " |
| 265 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 266 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 269 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 270 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 271 | intent.setComponent(admin.info.getComponent()); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 272 | mContext.sendBroadcast(intent); |
| 273 | } |
| 274 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 275 | void sendAdminCommandLocked(String action, int reqPolicy) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 276 | final int N = mAdminList.size(); |
| 277 | if (N > 0) { |
| 278 | for (int i=0; i<N; i++) { |
| 279 | ActiveAdmin admin = mAdminList.get(i); |
| 280 | if (admin.info.usesPolicy(reqPolicy)) { |
| 281 | sendAdminCommandLocked(admin, action); |
| 282 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 283 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 287 | void removeActiveAdminLocked(ComponentName adminReceiver) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 288 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 289 | if (admin != null) { |
| 290 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 291 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 292 | // XXX need to wait for it to complete. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 293 | mAdminList.remove(admin); |
| 294 | mAdminMap.remove(adminReceiver); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 295 | validatePasswordOwnerLocked(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | public DeviceAdminInfo findAdmin(ComponentName adminName) { |
| 300 | Intent resolveIntent = new Intent(); |
| 301 | resolveIntent.setComponent(adminName); |
| 302 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
| 303 | resolveIntent, PackageManager.GET_META_DATA); |
| 304 | if (infos == null || infos.size() <= 0) { |
| 305 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 306 | } |
| 307 | |
| 308 | try { |
| 309 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 310 | } catch (XmlPullParserException e) { |
| 311 | Log.w(TAG, "Bad device admin requested: " + adminName, e); |
| 312 | return null; |
| 313 | } catch (IOException e) { |
| 314 | Log.w(TAG, "Bad device admin requested: " + adminName, e); |
| 315 | return null; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | private static JournaledFile makeJournaledFile() { |
| 320 | final String base = "/data/system/device_policies.xml"; |
| 321 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 322 | } |
| 323 | |
| 324 | private void saveSettingsLocked() { |
| 325 | JournaledFile journal = makeJournaledFile(); |
| 326 | FileOutputStream stream = null; |
| 327 | try { |
| 328 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 329 | XmlSerializer out = new FastXmlSerializer(); |
| 330 | out.setOutput(stream, "utf-8"); |
| 331 | out.startDocument(null, true); |
| 332 | |
| 333 | out.startTag(null, "policies"); |
| 334 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 335 | final int N = mAdminList.size(); |
| 336 | for (int i=0; i<N; i++) { |
| 337 | ActiveAdmin ap = mAdminList.get(i); |
| 338 | if (ap != null) { |
| 339 | out.startTag(null, "admin"); |
| 340 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 341 | ap.writeToXml(out); |
| 342 | out.endTag(null, "admin"); |
| 343 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 344 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 345 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 346 | out.endTag(null, "policies"); |
| 347 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 348 | if (mPasswordOwner >= 0) { |
| 349 | out.startTag(null, "password-owner"); |
| 350 | out.attribute(null, "value", Integer.toString(mPasswordOwner)); |
| 351 | out.endTag(null, "password-owner"); |
| 352 | } |
| 353 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 354 | if (mFailedPasswordAttempts != 0) { |
| 355 | out.startTag(null, "failed-password-attempts"); |
| 356 | out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts)); |
| 357 | out.endTag(null, "failed-password-attempts"); |
| 358 | } |
| 359 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 360 | out.endDocument(); |
| 361 | stream.close(); |
| 362 | journal.commit(); |
| 363 | } catch (IOException e) { |
| 364 | try { |
| 365 | if (stream != null) { |
| 366 | stream.close(); |
| 367 | } |
| 368 | } catch (IOException ex) { |
| 369 | // Ignore |
| 370 | } |
| 371 | journal.rollback(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private void loadSettingsLocked() { |
| 376 | JournaledFile journal = makeJournaledFile(); |
| 377 | FileInputStream stream = null; |
| 378 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 379 | try { |
| 380 | stream = new FileInputStream(file); |
| 381 | XmlPullParser parser = Xml.newPullParser(); |
| 382 | parser.setInput(stream, null); |
| 383 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 384 | int type; |
| 385 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 386 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 387 | } |
| 388 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 389 | if (!"policies".equals(tag)) { |
| 390 | throw new XmlPullParserException( |
| 391 | "Settings do not start with policies tag: found " + tag); |
| 392 | } |
| 393 | type = parser.next(); |
| 394 | int outerDepth = parser.getDepth(); |
| 395 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 396 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 397 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 398 | continue; |
| 399 | } |
| 400 | tag = parser.getName(); |
| 401 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 402 | String name = parser.getAttributeValue(null, "name"); |
| 403 | try { |
| 404 | DeviceAdminInfo dai = findAdmin( |
| 405 | ComponentName.unflattenFromString(name)); |
| 406 | if (dai != null) { |
| 407 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 408 | ap.readFromXml(parser); |
| 409 | mAdminMap.put(ap.info.getComponent(), ap); |
| 410 | mAdminList.add(ap); |
| 411 | } |
| 412 | } catch (RuntimeException e) { |
| 413 | Log.w(TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 414 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 415 | } else if ("failed-password-attempts".equals(tag)) { |
| 416 | mFailedPasswordAttempts = Integer.parseInt( |
| 417 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 418 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 419 | } else if ("password-owner".equals(tag)) { |
| 420 | mPasswordOwner = Integer.parseInt( |
| 421 | parser.getAttributeValue(null, "value")); |
| 422 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 423 | } else { |
| 424 | Log.w(TAG, "Unknown tag: " + tag); |
| 425 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 426 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 427 | } |
| 428 | } catch (NullPointerException e) { |
| 429 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 430 | } catch (NumberFormatException e) { |
| 431 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 432 | } catch (XmlPullParserException e) { |
| 433 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 434 | } catch (IOException e) { |
| 435 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 436 | } catch (IndexOutOfBoundsException e) { |
| 437 | Log.w(TAG, "failed parsing " + file + " " + e); |
| 438 | } |
| 439 | try { |
| 440 | if (stream != null) { |
| 441 | stream.close(); |
| 442 | } |
| 443 | } catch (IOException e) { |
| 444 | // Ignore |
| 445 | } |
| 446 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 447 | validatePasswordOwnerLocked(); |
| 448 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 449 | long timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 450 | if (timeMs <= 0) { |
| 451 | timeMs = Integer.MAX_VALUE; |
| 452 | } |
| 453 | try { |
| 454 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 455 | } catch (RemoteException e) { |
| 456 | Log.w(TAG, "Failure talking with power manager", e); |
| 457 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 460 | void validatePasswordOwnerLocked() { |
| 461 | if (mPasswordOwner >= 0) { |
| 462 | boolean haveOwner = false; |
| 463 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 464 | if (mAdminList.get(i).getUid() == mPasswordOwner) { |
| 465 | haveOwner = true; |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | if (!haveOwner) { |
| 470 | Log.w(TAG, "Previous password owner " + mPasswordOwner |
| 471 | + " no longer active; disabling"); |
| 472 | mPasswordOwner = -1; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 477 | public void systemReady() { |
| 478 | synchronized (this) { |
| 479 | loadSettingsLocked(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | public void setActiveAdmin(ComponentName adminReceiver) { |
| 484 | mContext.enforceCallingOrSelfPermission( |
| 485 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 486 | |
| 487 | DeviceAdminInfo info = findAdmin(adminReceiver); |
| 488 | if (info == null) { |
| 489 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 490 | } |
| 491 | synchronized (this) { |
| 492 | long ident = Binder.clearCallingIdentity(); |
| 493 | try { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 494 | if (getActiveAdminUncheckedLocked(adminReceiver) != null) { |
| 495 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 496 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 497 | ActiveAdmin admin = new ActiveAdmin(info); |
| 498 | mAdminMap.put(adminReceiver, admin); |
| 499 | mAdminList.add(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 500 | saveSettingsLocked(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 501 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 502 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 503 | } finally { |
| 504 | Binder.restoreCallingIdentity(ident); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 509 | public boolean isAdminActive(ComponentName adminReceiver) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 510 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 511 | return getActiveAdminUncheckedLocked(adminReceiver) != null; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | public List<ComponentName> getActiveAdmins() { |
| 516 | synchronized (this) { |
| 517 | final int N = mAdminList.size(); |
| 518 | if (N <= 0) { |
| 519 | return null; |
| 520 | } |
| 521 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 522 | for (int i=0; i<N; i++) { |
| 523 | res.add(mAdminList.get(i).info.getComponent()); |
| 524 | } |
| 525 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 529 | public boolean packageHasActiveAdmins(String packageName) { |
| 530 | synchronized (this) { |
| 531 | final int N = mAdminList.size(); |
| 532 | for (int i=0; i<N; i++) { |
| 533 | if (mAdminList.get(i).info.getPackageName().equals(packageName)) { |
| 534 | return true; |
| 535 | } |
| 536 | } |
| 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 541 | public void removeActiveAdmin(ComponentName adminReceiver) { |
| 542 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 543 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 544 | if (admin == null) { |
| 545 | return; |
| 546 | } |
| 547 | if (admin.getUid() != Binder.getCallingUid()) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 548 | mContext.enforceCallingOrSelfPermission( |
| 549 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 550 | } |
| 551 | long ident = Binder.clearCallingIdentity(); |
| 552 | try { |
| 553 | removeActiveAdminLocked(adminReceiver); |
| 554 | } finally { |
| 555 | Binder.restoreCallingIdentity(ident); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 560 | public void setPasswordQuality(ComponentName who, int mode) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 561 | synchronized (this) { |
| 562 | if (who == null) { |
| 563 | throw new NullPointerException("ComponentName is null"); |
| 564 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 565 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 566 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 567 | if (ap.passwordQuality != mode) { |
| 568 | ap.passwordQuality = mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 569 | saveSettingsLocked(); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 574 | public int getPasswordQuality(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 575 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 576 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 577 | |
| 578 | if (who != null) { |
| 579 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 580 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 584 | for (int i=0; i<N; i++) { |
| 585 | ActiveAdmin admin = mAdminList.get(i); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 586 | if (mode < admin.passwordQuality) { |
| 587 | mode = admin.passwordQuality; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 594 | public void setPasswordMinimumLength(ComponentName who, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 595 | synchronized (this) { |
| 596 | if (who == null) { |
| 597 | throw new NullPointerException("ComponentName is null"); |
| 598 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 599 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 600 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 601 | if (ap.minimumPasswordLength != length) { |
| 602 | ap.minimumPasswordLength = length; |
| 603 | saveSettingsLocked(); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 608 | public int getPasswordMinimumLength(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 609 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 610 | int length = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 611 | |
| 612 | if (who != null) { |
| 613 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 614 | return admin != null ? admin.minimumPasswordLength : length; |
| 615 | } |
| 616 | |
| 617 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 618 | for (int i=0; i<N; i++) { |
| 619 | ActiveAdmin admin = mAdminList.get(i); |
| 620 | if (length < admin.minimumPasswordLength) { |
| 621 | length = admin.minimumPasswordLength; |
| 622 | } |
| 623 | } |
| 624 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 628 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 629 | synchronized (this) { |
| 630 | // This API can only be called by an active device admin, |
| 631 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 632 | getActiveAdminForCallerLocked(null, |
| 633 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 634 | return mActivePasswordQuality >= getPasswordQuality(null) |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 635 | && mActivePasswordLength >= getPasswordMinimumLength(null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | public int getCurrentFailedPasswordAttempts() { |
| 640 | synchronized (this) { |
| 641 | // This API can only be called by an active device admin, |
| 642 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 643 | getActiveAdminForCallerLocked(null, |
| 644 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 645 | return mFailedPasswordAttempts; |
| 646 | } |
| 647 | } |
| 648 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 649 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) { |
| 650 | synchronized (this) { |
| 651 | // This API can only be called by an active device admin, |
| 652 | // so try to retrieve it to check that the caller is one. |
| 653 | getActiveAdminForCallerLocked(who, |
| 654 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 655 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 656 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 657 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 658 | ap.maximumFailedPasswordsForWipe = num; |
| 659 | saveSettingsLocked(); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 664 | public int getMaximumFailedPasswordsForWipe(ComponentName who) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 665 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 666 | int count = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 667 | |
| 668 | if (who != null) { |
| 669 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 670 | return admin != null ? admin.maximumFailedPasswordsForWipe : count; |
| 671 | } |
| 672 | |
| 673 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 674 | for (int i=0; i<N; i++) { |
| 675 | ActiveAdmin admin = mAdminList.get(i); |
| 676 | if (count == 0) { |
| 677 | count = admin.maximumFailedPasswordsForWipe; |
| 678 | } else if (admin.maximumFailedPasswordsForWipe != 0 |
| 679 | && count > admin.maximumFailedPasswordsForWipe) { |
| 680 | count = admin.maximumFailedPasswordsForWipe; |
| 681 | } |
| 682 | } |
| 683 | return count; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 687 | public boolean resetPassword(String password, int flags) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 688 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 689 | synchronized (this) { |
| 690 | // This API can only be called by an active device admin, |
| 691 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 692 | getActiveAdminForCallerLocked(null, |
| 693 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 694 | quality = getPasswordQuality(null); |
| 695 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 696 | int adjQuality = LockPatternUtils.adjustPasswordMode(password, quality); |
| 697 | if (adjQuality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 698 | Log.w(TAG, "resetPassword: password does not meet quality " + quality); |
| 699 | return false; |
| 700 | } |
| 701 | quality = adjQuality; |
| 702 | } |
| 703 | int length = getPasswordMinimumLength(null); |
| 704 | if (password.length() < length) { |
| 705 | Log.w(TAG, "resetPassword: password does not meet length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | } |
| 709 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 710 | int callingUid = Binder.getCallingUid(); |
| 711 | if (mPasswordOwner >= 0 && mPasswordOwner != callingUid) { |
| 712 | Log.w(TAG, "resetPassword: already set by another uid and not entered by user"); |
| 713 | return false; |
| 714 | } |
| 715 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 716 | // Don't do this with the lock held, because it is going to call |
| 717 | // back in to the service. |
| 718 | long ident = Binder.clearCallingIdentity(); |
| 719 | try { |
| 720 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 721 | utils.saveLockPassword(password, quality); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 722 | int newOwner = (flags&DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY) |
| 723 | != 0 ? callingUid : -1; |
| 724 | if (mPasswordOwner != newOwner) { |
| 725 | mPasswordOwner = newOwner; |
| 726 | saveSettingsLocked(); |
| 727 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 728 | } finally { |
| 729 | Binder.restoreCallingIdentity(ident); |
| 730 | } |
| 731 | |
| 732 | return true; |
| 733 | } |
| 734 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 735 | public void setMaximumTimeToLock(ComponentName who, long timeMs) { |
| 736 | synchronized (this) { |
| 737 | if (who == null) { |
| 738 | throw new NullPointerException("ComponentName is null"); |
| 739 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 740 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 741 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 742 | if (ap.maximumTimeToUnlock != timeMs) { |
| 743 | ap.maximumTimeToUnlock = timeMs; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 744 | |
| 745 | long ident = Binder.clearCallingIdentity(); |
| 746 | try { |
| 747 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 748 | |
| 749 | timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 750 | if (timeMs <= 0) { |
| 751 | timeMs = Integer.MAX_VALUE; |
| 752 | } |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 753 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 754 | try { |
| 755 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 756 | } catch (RemoteException e) { |
| 757 | Log.w(TAG, "Failure talking with power manager", e); |
| 758 | } |
| 759 | } finally { |
| 760 | Binder.restoreCallingIdentity(ident); |
| 761 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 766 | public long getMaximumTimeToLock(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 767 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 768 | long time = 0; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 769 | |
| 770 | if (who != null) { |
| 771 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 772 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 773 | } |
| 774 | |
| 775 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 776 | for (int i=0; i<N; i++) { |
| 777 | ActiveAdmin admin = mAdminList.get(i); |
| 778 | if (time == 0) { |
| 779 | time = admin.maximumTimeToUnlock; |
| 780 | } else if (admin.maximumTimeToUnlock != 0 |
| 781 | && time > admin.maximumTimeToUnlock) { |
| 782 | time = admin.maximumTimeToUnlock; |
| 783 | } |
| 784 | } |
| 785 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 789 | public void lockNow() { |
| 790 | synchronized (this) { |
| 791 | // This API can only be called by an active device admin, |
| 792 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 793 | getActiveAdminForCallerLocked(null, |
| 794 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 795 | long ident = Binder.clearCallingIdentity(); |
| 796 | try { |
| 797 | mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(), |
| 798 | WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN); |
| 799 | } catch (RemoteException e) { |
| 800 | } finally { |
| 801 | Binder.restoreCallingIdentity(ident); |
| 802 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 806 | void wipeDataLocked(int flags) { |
| 807 | try { |
| 808 | RecoverySystem.rebootWipeUserData(mContext); |
| 809 | } catch (IOException e) { |
| 810 | Log.w(TAG, "Failed requesting data wipe", e); |
| 811 | } |
| 812 | } |
| 813 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 814 | public void wipeData(int flags) { |
| 815 | synchronized (this) { |
| 816 | // This API can only be called by an active device admin, |
| 817 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 818 | getActiveAdminForCallerLocked(null, |
| 819 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 820 | long ident = Binder.clearCallingIdentity(); |
| 821 | try { |
| 822 | wipeDataLocked(flags); |
| 823 | } finally { |
| 824 | Binder.restoreCallingIdentity(ident); |
| 825 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 826 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result) { |
| 830 | mContext.enforceCallingOrSelfPermission( |
| 831 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 832 | |
| 833 | synchronized (this) { |
| 834 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp); |
| 835 | if (admin == null) { |
| 836 | try { |
| 837 | result.sendResult(null); |
| 838 | } catch (RemoteException e) { |
| 839 | } |
| 840 | return; |
| 841 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 842 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 843 | intent.setComponent(admin.info.getComponent()); |
| 844 | mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { |
| 845 | @Override |
| 846 | public void onReceive(Context context, Intent intent) { |
| 847 | try { |
| 848 | result.sendResult(getResultExtras(false)); |
| 849 | } catch (RemoteException e) { |
| 850 | } |
| 851 | } |
| 852 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 856 | public void setActivePasswordState(int quality, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 857 | mContext.enforceCallingOrSelfPermission( |
| 858 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 859 | |
| 860 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 861 | if (mActivePasswordQuality != quality || mActivePasswordLength != length |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 862 | || mFailedPasswordAttempts != 0) { |
| 863 | long ident = Binder.clearCallingIdentity(); |
| 864 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 865 | mActivePasswordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 866 | mActivePasswordLength = length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 867 | if (mFailedPasswordAttempts != 0) { |
| 868 | mFailedPasswordAttempts = 0; |
| 869 | saveSettingsLocked(); |
| 870 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 871 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 872 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 873 | } finally { |
| 874 | Binder.restoreCallingIdentity(ident); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | public void reportFailedPasswordAttempt() { |
| 881 | mContext.enforceCallingOrSelfPermission( |
| 882 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 883 | |
| 884 | synchronized (this) { |
| 885 | long ident = Binder.clearCallingIdentity(); |
| 886 | try { |
| 887 | mFailedPasswordAttempts++; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 888 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 889 | int max = getMaximumFailedPasswordsForWipe(null); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 890 | if (max > 0 && mFailedPasswordAttempts >= max) { |
| 891 | wipeDataLocked(0); |
| 892 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 893 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 894 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 895 | } finally { |
| 896 | Binder.restoreCallingIdentity(ident); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | public void reportSuccessfulPasswordAttempt() { |
| 902 | mContext.enforceCallingOrSelfPermission( |
| 903 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 904 | |
| 905 | synchronized (this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 906 | if (mFailedPasswordAttempts != 0 || mPasswordOwner >= 0) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 907 | long ident = Binder.clearCallingIdentity(); |
| 908 | try { |
| 909 | mFailedPasswordAttempts = 0; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 910 | mPasswordOwner = -1; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 911 | saveSettingsLocked(); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 912 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 913 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 914 | } finally { |
| 915 | Binder.restoreCallingIdentity(ident); |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 920 | |
| 921 | @Override |
| 922 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 923 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 924 | != PackageManager.PERMISSION_GRANTED) { |
| 925 | |
| 926 | pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid=" |
| 927 | + Binder.getCallingPid() |
| 928 | + ", uid=" + Binder.getCallingUid()); |
| 929 | return; |
| 930 | } |
| 931 | |
| 932 | final Printer p = new PrintWriterPrinter(pw); |
| 933 | |
| 934 | synchronized (this) { |
| 935 | p.println("Current Device Policy Manager state:"); |
| 936 | |
| 937 | p.println(" Enabled Device Admins:"); |
| 938 | final int N = mAdminList.size(); |
| 939 | for (int i=0; i<N; i++) { |
| 940 | ActiveAdmin ap = mAdminList.get(i); |
| 941 | if (ap != null) { |
| 942 | pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString()); |
| 943 | pw.println(":"); |
| 944 | ap.dump(" ", pw); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | pw.println(" "); |
| 949 | pw.print(" mActivePasswordQuality="); pw.println(mActivePasswordQuality); |
| 950 | pw.print(" mActivePasswordLength="); pw.println(mActivePasswordLength); |
| 951 | pw.print(" mFailedPasswordAttempts="); pw.println(mFailedPasswordAttempts); |
| 952 | pw.print(" mPasswordOwner="); pw.println(mPasswordOwner); |
| 953 | } |
| 954 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 955 | } |