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