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 | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 20 | import com.android.internal.os.storage.ExternalStorageFormatter; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 21 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 22 | import com.android.internal.util.JournaledFile; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 23 | import com.android.internal.util.XmlUtils; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 24 | import com.android.internal.widget.LockPatternUtils; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 25 | |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | import org.xmlpull.v1.XmlSerializer; |
| 29 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 30 | import android.app.Activity; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 31 | import android.app.AlarmManager; |
| 32 | import android.app.PendingIntent; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 33 | import android.app.admin.DeviceAdminInfo; |
| 34 | import android.app.admin.DeviceAdminReceiver; |
| 35 | import android.app.admin.DevicePolicyManager; |
| 36 | import android.app.admin.IDevicePolicyManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 38 | import android.content.ComponentName; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager.NameNotFoundException; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 45 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 47 | import android.os.Environment; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 48 | import android.os.Handler; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 49 | import android.os.IBinder; |
| 50 | import android.os.IPowerManager; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 51 | import android.os.PowerManager; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 52 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 53 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 54 | import android.os.RemoteException; |
| 55 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 56 | import android.os.SystemClock; |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 57 | import android.os.SystemProperties; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 58 | import android.provider.Settings; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 59 | import android.util.PrintWriterPrinter; |
| 60 | import android.util.Printer; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 61 | import android.util.Slog; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 62 | import android.util.Xml; |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 63 | import android.view.IWindowManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 64 | import android.view.WindowManagerPolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 65 | |
| 66 | import java.io.File; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 67 | import java.io.FileDescriptor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 68 | import java.io.FileInputStream; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 69 | import java.io.FileNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 70 | import java.io.FileOutputStream; |
| 71 | import java.io.IOException; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 72 | import java.io.PrintWriter; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 73 | import java.text.DateFormat; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 74 | import java.util.ArrayList; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 75 | import java.util.Date; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 76 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 77 | import java.util.List; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 78 | import java.util.Set; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Implementation of the device policy APIs. |
| 82 | */ |
| 83 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 84 | private static final String TAG = "DevicePolicyManagerService"; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 85 | |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 86 | private static final int REQUEST_EXPIRE_PASSWORD = 5571; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 87 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 88 | private static final long EXPIRATION_GRACE_PERIOD_MS = 5 * 86400 * 1000; // 5 days, in ms |
| 89 | |
| 90 | protected static final String ACTION_EXPIRED_PASSWORD_NOTIFICATION |
| 91 | = "com.android.server.ACTION_EXPIRED_PASSWORD_NOTIFICATION"; |
| 92 | |
| 93 | private static final long MS_PER_DAY = 86400 * 1000; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 94 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 95 | final Context mContext; |
| 96 | final MyPackageMonitor mMonitor; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 97 | final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 98 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 99 | IPowerManager mIPowerManager; |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 100 | IWindowManager mIWindowManager; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 101 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 102 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 103 | int mActivePasswordLength = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 104 | int mActivePasswordUpperCase = 0; |
| 105 | int mActivePasswordLowerCase = 0; |
| 106 | int mActivePasswordLetters = 0; |
| 107 | int mActivePasswordNumeric = 0; |
| 108 | int mActivePasswordSymbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 109 | int mActivePasswordNonLetter = 0; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 110 | int mFailedPasswordAttempts = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 111 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 112 | int mPasswordOwner = -1; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 113 | Handler mHandler = new Handler(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 114 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 115 | long mLastMaximumTimeToLock = -1; |
| 116 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 117 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 118 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 119 | final ArrayList<ActiveAdmin> mAdminList |
| 120 | = new ArrayList<ActiveAdmin>(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 121 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 122 | BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 123 | @Override |
| 124 | public void onReceive(Context context, Intent intent) { |
| 125 | String action = intent.getAction(); |
| 126 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) |
| 127 | || ACTION_EXPIRED_PASSWORD_NOTIFICATION.equals(action)) { |
| 128 | Slog.v(TAG, "Sending password expiration notifications for action " + action); |
| 129 | mHandler.post(new Runnable() { |
| 130 | public void run() { |
| 131 | handlePasswordExpirationNotification(); |
| 132 | } |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 | }; |
| 137 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 138 | static class ActiveAdmin { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 139 | final DeviceAdminInfo info; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 140 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 141 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 142 | |
| 143 | static final int DEF_MINIMUM_PASSWORD_LENGTH = 0; |
| 144 | int minimumPasswordLength = DEF_MINIMUM_PASSWORD_LENGTH; |
| 145 | |
| 146 | static final int DEF_PASSWORD_HISTORY_LENGTH = 0; |
| 147 | int passwordHistoryLength = DEF_PASSWORD_HISTORY_LENGTH; |
| 148 | |
| 149 | static final int DEF_MINIMUM_PASSWORD_UPPER_CASE = 0; |
| 150 | int minimumPasswordUpperCase = DEF_MINIMUM_PASSWORD_UPPER_CASE; |
| 151 | |
| 152 | static final int DEF_MINIMUM_PASSWORD_LOWER_CASE = 0; |
| 153 | int minimumPasswordLowerCase = DEF_MINIMUM_PASSWORD_LOWER_CASE; |
| 154 | |
| 155 | static final int DEF_MINIMUM_PASSWORD_LETTERS = 1; |
Dianne Hackborn | 2a10840 | 2011-05-26 11:45:00 -0700 | [diff] [blame] | 156 | int minimumPasswordLetters = DEF_MINIMUM_PASSWORD_LETTERS; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 157 | |
| 158 | static final int DEF_MINIMUM_PASSWORD_NUMERIC = 1; |
| 159 | int minimumPasswordNumeric = DEF_MINIMUM_PASSWORD_NUMERIC; |
| 160 | |
| 161 | static final int DEF_MINIMUM_PASSWORD_SYMBOLS = 1; |
| 162 | int minimumPasswordSymbols = DEF_MINIMUM_PASSWORD_SYMBOLS; |
| 163 | |
| 164 | static final int DEF_MINIMUM_PASSWORD_NON_LETTER = 0; |
| 165 | int minimumPasswordNonLetter = DEF_MINIMUM_PASSWORD_NON_LETTER; |
| 166 | |
| 167 | static final long DEF_MAXIMUM_TIME_TO_UNLOCK = 0; |
| 168 | long maximumTimeToUnlock = DEF_MAXIMUM_TIME_TO_UNLOCK; |
| 169 | |
| 170 | static final int DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = 0; |
| 171 | int maximumFailedPasswordsForWipe = DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE; |
| 172 | |
| 173 | static final long DEF_PASSWORD_EXPIRATION_TIMEOUT = 0; |
| 174 | long passwordExpirationTimeout = DEF_PASSWORD_EXPIRATION_TIMEOUT; |
| 175 | |
| 176 | static final long DEF_PASSWORD_EXPIRATION_DATE = 0; |
| 177 | long passwordExpirationDate = DEF_PASSWORD_EXPIRATION_DATE; |
| 178 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 179 | boolean encryptionRequested = false; |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 180 | boolean disableCamera = false; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 181 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 182 | // TODO: review implementation decisions with frameworks team |
| 183 | boolean specifiesGlobalProxy = false; |
| 184 | String globalProxySpec = null; |
| 185 | String globalProxyExclusionList = null; |
| 186 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 187 | ActiveAdmin(DeviceAdminInfo _info) { |
| 188 | info = _info; |
| 189 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 190 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 191 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 192 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 193 | void writeToXml(XmlSerializer out) |
| 194 | throws IllegalArgumentException, IllegalStateException, IOException { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 195 | out.startTag(null, "policies"); |
| 196 | info.writePoliciesToXml(out); |
| 197 | out.endTag(null, "policies"); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 198 | if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 199 | out.startTag(null, "password-quality"); |
| 200 | out.attribute(null, "value", Integer.toString(passwordQuality)); |
| 201 | out.endTag(null, "password-quality"); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 202 | if (minimumPasswordLength != DEF_MINIMUM_PASSWORD_LENGTH) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 203 | out.startTag(null, "min-password-length"); |
| 204 | out.attribute(null, "value", Integer.toString(minimumPasswordLength)); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 205 | out.endTag(null, "min-password-length"); |
| 206 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 207 | if(passwordHistoryLength != DEF_PASSWORD_HISTORY_LENGTH) { |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 208 | out.startTag(null, "password-history-length"); |
| 209 | out.attribute(null, "value", Integer.toString(passwordHistoryLength)); |
| 210 | out.endTag(null, "password-history-length"); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 211 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 212 | if (minimumPasswordUpperCase != DEF_MINIMUM_PASSWORD_UPPER_CASE) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 213 | out.startTag(null, "min-password-uppercase"); |
| 214 | out.attribute(null, "value", Integer.toString(minimumPasswordUpperCase)); |
| 215 | out.endTag(null, "min-password-uppercase"); |
| 216 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 217 | if (minimumPasswordLowerCase != DEF_MINIMUM_PASSWORD_LOWER_CASE) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 218 | out.startTag(null, "min-password-lowercase"); |
| 219 | out.attribute(null, "value", Integer.toString(minimumPasswordLowerCase)); |
| 220 | out.endTag(null, "min-password-lowercase"); |
| 221 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 222 | if (minimumPasswordLetters != DEF_MINIMUM_PASSWORD_LETTERS) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 223 | out.startTag(null, "min-password-letters"); |
| 224 | out.attribute(null, "value", Integer.toString(minimumPasswordLetters)); |
| 225 | out.endTag(null, "min-password-letters"); |
| 226 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 227 | if (minimumPasswordNumeric != DEF_MINIMUM_PASSWORD_NUMERIC) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 228 | out.startTag(null, "min-password-numeric"); |
| 229 | out.attribute(null, "value", Integer.toString(minimumPasswordNumeric)); |
| 230 | out.endTag(null, "min-password-numeric"); |
| 231 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 232 | if (minimumPasswordSymbols != DEF_MINIMUM_PASSWORD_SYMBOLS) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 233 | out.startTag(null, "min-password-symbols"); |
| 234 | out.attribute(null, "value", Integer.toString(minimumPasswordSymbols)); |
| 235 | out.endTag(null, "min-password-symbols"); |
| 236 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 237 | if (minimumPasswordNonLetter > DEF_MINIMUM_PASSWORD_NON_LETTER) { |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 238 | out.startTag(null, "min-password-nonletter"); |
| 239 | out.attribute(null, "value", Integer.toString(minimumPasswordNonLetter)); |
| 240 | out.endTag(null, "min-password-nonletter"); |
| 241 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 242 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 243 | if (maximumTimeToUnlock != DEF_MAXIMUM_TIME_TO_UNLOCK) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 244 | out.startTag(null, "max-time-to-unlock"); |
| 245 | out.attribute(null, "value", Long.toString(maximumTimeToUnlock)); |
| 246 | out.endTag(null, "max-time-to-unlock"); |
| 247 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 248 | if (maximumFailedPasswordsForWipe != DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 249 | out.startTag(null, "max-failed-password-wipe"); |
| 250 | out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe)); |
| 251 | out.endTag(null, "max-failed-password-wipe"); |
| 252 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 253 | if (specifiesGlobalProxy) { |
| 254 | out.startTag(null, "specifies-global-proxy"); |
| 255 | out.attribute(null, "value", Boolean.toString(specifiesGlobalProxy)); |
| 256 | out.endTag(null, "specifies_global_proxy"); |
| 257 | if (globalProxySpec != null) { |
| 258 | out.startTag(null, "global-proxy-spec"); |
| 259 | out.attribute(null, "value", globalProxySpec); |
| 260 | out.endTag(null, "global-proxy-spec"); |
| 261 | } |
| 262 | if (globalProxyExclusionList != null) { |
| 263 | out.startTag(null, "global-proxy-exclusion-list"); |
| 264 | out.attribute(null, "value", globalProxyExclusionList); |
| 265 | out.endTag(null, "global-proxy-exclusion-list"); |
| 266 | } |
| 267 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 268 | if (passwordExpirationTimeout != DEF_PASSWORD_EXPIRATION_TIMEOUT) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 269 | out.startTag(null, "password-expiration-timeout"); |
| 270 | out.attribute(null, "value", Long.toString(passwordExpirationTimeout)); |
| 271 | out.endTag(null, "password-expiration-timeout"); |
| 272 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 273 | if (passwordExpirationDate != DEF_PASSWORD_EXPIRATION_DATE) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 274 | out.startTag(null, "password-expiration-date"); |
| 275 | out.attribute(null, "value", Long.toString(passwordExpirationDate)); |
| 276 | out.endTag(null, "password-expiration-date"); |
| 277 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 278 | if (encryptionRequested) { |
| 279 | out.startTag(null, "encryption-requested"); |
| 280 | out.attribute(null, "value", Boolean.toString(encryptionRequested)); |
| 281 | out.endTag(null, "encryption-requested"); |
| 282 | } |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 283 | if (disableCamera) { |
| 284 | out.startTag(null, "disable-camera"); |
| 285 | out.attribute(null, "value", Boolean.toString(disableCamera)); |
| 286 | out.endTag(null, "disable-camera"); |
| 287 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 288 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 289 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 290 | void readFromXml(XmlPullParser parser) |
| 291 | throws XmlPullParserException, IOException { |
| 292 | int outerDepth = parser.getDepth(); |
| 293 | int type; |
| 294 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 295 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 296 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 297 | continue; |
| 298 | } |
| 299 | String tag = parser.getName(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 300 | if ("policies".equals(tag)) { |
| 301 | info.readPoliciesFromXml(parser); |
| 302 | } else if ("password-quality".equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 303 | passwordQuality = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 304 | parser.getAttributeValue(null, "value")); |
| 305 | } else if ("min-password-length".equals(tag)) { |
| 306 | minimumPasswordLength = Integer.parseInt( |
| 307 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 308 | } else if ("password-history-length".equals(tag)) { |
| 309 | passwordHistoryLength = Integer.parseInt( |
| 310 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 311 | } else if ("min-password-uppercase".equals(tag)) { |
| 312 | minimumPasswordUpperCase = Integer.parseInt( |
| 313 | parser.getAttributeValue(null, "value")); |
| 314 | } else if ("min-password-lowercase".equals(tag)) { |
| 315 | minimumPasswordLowerCase = Integer.parseInt( |
| 316 | parser.getAttributeValue(null, "value")); |
| 317 | } else if ("min-password-letters".equals(tag)) { |
| 318 | minimumPasswordLetters = Integer.parseInt( |
| 319 | parser.getAttributeValue(null, "value")); |
| 320 | } else if ("min-password-numeric".equals(tag)) { |
| 321 | minimumPasswordNumeric = Integer.parseInt( |
| 322 | parser.getAttributeValue(null, "value")); |
| 323 | } else if ("min-password-symbols".equals(tag)) { |
| 324 | minimumPasswordSymbols = Integer.parseInt( |
| 325 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 326 | } else if ("min-password-nonletter".equals(tag)) { |
| 327 | minimumPasswordNonLetter = Integer.parseInt( |
| 328 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 329 | } else if ("max-time-to-unlock".equals(tag)) { |
| 330 | maximumTimeToUnlock = Long.parseLong( |
| 331 | parser.getAttributeValue(null, "value")); |
| 332 | } else if ("max-failed-password-wipe".equals(tag)) { |
| 333 | maximumFailedPasswordsForWipe = Integer.parseInt( |
| 334 | parser.getAttributeValue(null, "value")); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 335 | } else if ("specifies-global-proxy".equals(tag)) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 336 | specifiesGlobalProxy = Boolean.parseBoolean( |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 337 | parser.getAttributeValue(null, "value")); |
| 338 | } else if ("global-proxy-spec".equals(tag)) { |
| 339 | globalProxySpec = |
| 340 | parser.getAttributeValue(null, "value"); |
| 341 | } else if ("global-proxy-exclusion-list".equals(tag)) { |
| 342 | globalProxyExclusionList = |
| 343 | parser.getAttributeValue(null, "value"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 344 | } else if ("password-expiration-timeout".equals(tag)) { |
| 345 | passwordExpirationTimeout = Long.parseLong( |
| 346 | parser.getAttributeValue(null, "value")); |
| 347 | } else if ("password-expiration-date".equals(tag)) { |
| 348 | passwordExpirationDate = Long.parseLong( |
| 349 | parser.getAttributeValue(null, "value")); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 350 | } else if ("encryption-requested".equals(tag)) { |
| 351 | encryptionRequested = Boolean.parseBoolean( |
| 352 | parser.getAttributeValue(null, "value")); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 353 | } else if ("disable-camera".equals(tag)) { |
| 354 | disableCamera = Boolean.parseBoolean( |
| 355 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 356 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 357 | Slog.w(TAG, "Unknown admin tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 358 | } |
| 359 | XmlUtils.skipCurrentTag(parser); |
| 360 | } |
| 361 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 362 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 363 | void dump(String prefix, PrintWriter pw) { |
| 364 | pw.print(prefix); pw.print("uid="); pw.println(getUid()); |
| 365 | pw.print(prefix); pw.println("policies:"); |
| 366 | ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies(); |
| 367 | if (pols != null) { |
| 368 | for (int i=0; i<pols.size(); i++) { |
| 369 | pw.print(prefix); pw.print(" "); pw.println(pols.get(i).tag); |
| 370 | } |
| 371 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 372 | pw.print(prefix); pw.print("passwordQuality=0x"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 373 | pw.println(Integer.toHexString(passwordQuality)); |
| 374 | pw.print(prefix); pw.print("minimumPasswordLength="); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 375 | pw.println(minimumPasswordLength); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 376 | pw.print(prefix); pw.print("passwordHistoryLength="); |
| 377 | pw.println(passwordHistoryLength); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 378 | pw.print(prefix); pw.print("minimumPasswordUpperCase="); |
| 379 | pw.println(minimumPasswordUpperCase); |
| 380 | pw.print(prefix); pw.print("minimumPasswordLowerCase="); |
| 381 | pw.println(minimumPasswordLowerCase); |
| 382 | pw.print(prefix); pw.print("minimumPasswordLetters="); |
| 383 | pw.println(minimumPasswordLetters); |
| 384 | pw.print(prefix); pw.print("minimumPasswordNumeric="); |
| 385 | pw.println(minimumPasswordNumeric); |
| 386 | pw.print(prefix); pw.print("minimumPasswordSymbols="); |
| 387 | pw.println(minimumPasswordSymbols); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 388 | pw.print(prefix); pw.print("minimumPasswordNonLetter="); |
| 389 | pw.println(minimumPasswordNonLetter); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 390 | pw.print(prefix); pw.print("maximumTimeToUnlock="); |
| 391 | pw.println(maximumTimeToUnlock); |
| 392 | pw.print(prefix); pw.print("maximumFailedPasswordsForWipe="); |
| 393 | pw.println(maximumFailedPasswordsForWipe); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 394 | pw.print(prefix); pw.print("specifiesGlobalProxy="); |
| 395 | pw.println(specifiesGlobalProxy); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 396 | pw.print(prefix); pw.print("passwordExpirationTimeout="); |
| 397 | pw.println(passwordExpirationTimeout); |
| 398 | pw.print(prefix); pw.print("passwordExpirationDate="); |
| 399 | pw.println(passwordExpirationDate); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 400 | if (globalProxySpec != null) { |
| 401 | pw.print(prefix); pw.print("globalProxySpec="); |
| 402 | pw.println(globalProxySpec); |
| 403 | } |
| 404 | if (globalProxyExclusionList != null) { |
| 405 | pw.print(prefix); pw.print("globalProxyEclusionList="); |
| 406 | pw.println(globalProxyExclusionList); |
| 407 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 408 | pw.print(prefix); pw.print("encryptionRequested="); |
| 409 | pw.println(encryptionRequested); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 410 | pw.print(prefix); pw.print("disableCamera="); |
| 411 | pw.println(disableCamera); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 412 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 413 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 414 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 415 | class MyPackageMonitor extends PackageMonitor { |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 416 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 417 | public void onSomePackagesChanged() { |
| 418 | synchronized (DevicePolicyManagerService.this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 419 | boolean removed = false; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 420 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 421 | ActiveAdmin aa = mAdminList.get(i); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 422 | int change = isPackageDisappearing(aa.info.getPackageName()); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 423 | if (change == PACKAGE_PERMANENT_CHANGE |
| 424 | || change == PACKAGE_TEMPORARY_CHANGE) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 425 | Slog.w(TAG, "Admin unexpectedly uninstalled: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 426 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 427 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 428 | mAdminList.remove(i); |
| 429 | } else if (isPackageModified(aa.info.getPackageName())) { |
| 430 | try { |
| 431 | mContext.getPackageManager().getReceiverInfo( |
| 432 | aa.info.getComponent(), 0); |
| 433 | } catch (NameNotFoundException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 434 | Slog.w(TAG, "Admin package change removed component: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 435 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 436 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 437 | mAdminList.remove(i); |
| 438 | } |
| 439 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 440 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 441 | if (removed) { |
| 442 | validatePasswordOwnerLocked(); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 443 | syncDeviceCapabilitiesLocked(); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 444 | saveSettingsLocked(); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 445 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 446 | } |
| 447 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 448 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 449 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 450 | /** |
| 451 | * Instantiates the service. |
| 452 | */ |
| 453 | public DevicePolicyManagerService(Context context) { |
| 454 | mContext = context; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 455 | mMonitor = new MyPackageMonitor(); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 456 | mMonitor.register(context, null, true); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 457 | mWakeLock = ((PowerManager)context.getSystemService(Context.POWER_SERVICE)) |
| 458 | .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DPM"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 459 | IntentFilter filter = new IntentFilter(); |
| 460 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 461 | filter.addAction(ACTION_EXPIRED_PASSWORD_NOTIFICATION); |
| 462 | context.registerReceiver(mReceiver, filter); |
| 463 | } |
| 464 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 465 | /** |
| 466 | * Set an alarm for an upcoming event - expiration warning, expiration, or post-expiration |
| 467 | * reminders. Clears alarm if no expirations are configured. |
| 468 | */ |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 469 | protected void setExpirationAlarmCheckLocked(Context context) { |
| 470 | final long expiration = getPasswordExpirationLocked(null); |
| 471 | final long now = System.currentTimeMillis(); |
| 472 | final long timeToExpire = expiration - now; |
| 473 | final long alarmTime; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 474 | if (expiration == 0) { |
| 475 | // No expirations are currently configured: Cancel alarm. |
| 476 | alarmTime = 0; |
| 477 | } else if (timeToExpire <= 0) { |
| 478 | // The password has already expired: Repeat every 24 hours. |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 479 | alarmTime = now + MS_PER_DAY; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 480 | } else { |
| 481 | // Selecting the next alarm time: Roll forward to the next 24 hour multiple before |
| 482 | // the expiration time. |
| 483 | long alarmInterval = timeToExpire % MS_PER_DAY; |
| 484 | if (alarmInterval == 0) { |
| 485 | alarmInterval = MS_PER_DAY; |
| 486 | } |
| 487 | alarmTime = now + alarmInterval; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 490 | long token = Binder.clearCallingIdentity(); |
| 491 | try { |
| 492 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 493 | PendingIntent pi = PendingIntent.getBroadcast(context, REQUEST_EXPIRE_PASSWORD, |
| 494 | new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION), |
| 495 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); |
| 496 | am.cancel(pi); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 497 | if (alarmTime != 0) { |
| 498 | am.set(AlarmManager.RTC, alarmTime, pi); |
| 499 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 500 | } finally { |
| 501 | Binder.restoreCallingIdentity(token); |
| 502 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 505 | private IPowerManager getIPowerManager() { |
| 506 | if (mIPowerManager == null) { |
| 507 | IBinder b = ServiceManager.getService(Context.POWER_SERVICE); |
| 508 | mIPowerManager = IPowerManager.Stub.asInterface(b); |
| 509 | } |
| 510 | return mIPowerManager; |
| 511 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 512 | |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 513 | private IWindowManager getWindowManager() { |
| 514 | if (mIWindowManager == null) { |
| 515 | IBinder b = ServiceManager.getService(Context.WINDOW_SERVICE); |
| 516 | mIWindowManager = IWindowManager.Stub.asInterface(b); |
| 517 | } |
| 518 | return mIWindowManager; |
| 519 | } |
| 520 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 521 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 522 | ActiveAdmin admin = mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 523 | if (admin != null |
| 524 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 525 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 526 | return admin; |
| 527 | } |
| 528 | return null; |
| 529 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 530 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 531 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 532 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 533 | final int callingUid = Binder.getCallingUid(); |
| 534 | if (who != null) { |
| 535 | ActiveAdmin admin = mAdminMap.get(who); |
| 536 | if (admin == null) { |
| 537 | throw new SecurityException("No active admin " + who); |
| 538 | } |
| 539 | if (admin.getUid() != callingUid) { |
| 540 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 541 | + Binder.getCallingUid()); |
| 542 | } |
| 543 | if (!admin.info.usesPolicy(reqPolicy)) { |
| 544 | throw new SecurityException("Admin " + admin.info.getComponent() |
| 545 | + " did not specify uses-policy for: " |
| 546 | + admin.info.getTagForPolicy(reqPolicy)); |
| 547 | } |
| 548 | return admin; |
| 549 | } else { |
| 550 | final int N = mAdminList.size(); |
| 551 | for (int i=0; i<N; i++) { |
| 552 | ActiveAdmin admin = mAdminList.get(i); |
| 553 | if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) { |
| 554 | return admin; |
| 555 | } |
| 556 | } |
| 557 | throw new SecurityException("No active admin owned by uid " |
| 558 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 559 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 560 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 561 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 562 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 563 | sendAdminCommandLocked(admin, action, null); |
| 564 | } |
| 565 | |
| 566 | void sendAdminCommandLocked(ActiveAdmin admin, String action, BroadcastReceiver result) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 567 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 568 | intent.setComponent(admin.info.getComponent()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 569 | if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) { |
| 570 | intent.putExtra("expiration", admin.passwordExpirationDate); |
| 571 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 572 | if (result != null) { |
| 573 | mContext.sendOrderedBroadcast(intent, null, result, mHandler, |
| 574 | Activity.RESULT_OK, null, null); |
| 575 | } else { |
| 576 | mContext.sendBroadcast(intent); |
| 577 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 578 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 579 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 580 | void sendAdminCommandLocked(String action, int reqPolicy) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 581 | final int N = mAdminList.size(); |
| 582 | if (N > 0) { |
| 583 | for (int i=0; i<N; i++) { |
| 584 | ActiveAdmin admin = mAdminList.get(i); |
| 585 | if (admin.info.usesPolicy(reqPolicy)) { |
| 586 | sendAdminCommandLocked(admin, action); |
| 587 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 588 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 591 | |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 592 | void removeActiveAdminLocked(final ComponentName adminReceiver) { |
| 593 | final ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 594 | if (admin != null) { |
| 595 | sendAdminCommandLocked(admin, |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 596 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED, |
| 597 | new BroadcastReceiver() { |
| 598 | @Override |
| 599 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 600 | synchronized (DevicePolicyManagerService.this) { |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 601 | boolean doProxyCleanup = admin.info.usesPolicy( |
| 602 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
| 603 | mAdminList.remove(admin); |
| 604 | mAdminMap.remove(adminReceiver); |
| 605 | validatePasswordOwnerLocked(); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 606 | syncDeviceCapabilitiesLocked(); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 607 | if (doProxyCleanup) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 608 | resetGlobalProxyLocked(); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 609 | } |
| 610 | saveSettingsLocked(); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 611 | updateMaximumTimeToLockLocked(); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | }); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 615 | } |
| 616 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 617 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 618 | public DeviceAdminInfo findAdmin(ComponentName adminName) { |
| 619 | Intent resolveIntent = new Intent(); |
| 620 | resolveIntent.setComponent(adminName); |
| 621 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
| 622 | resolveIntent, PackageManager.GET_META_DATA); |
| 623 | if (infos == null || infos.size() <= 0) { |
| 624 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 625 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 626 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 627 | try { |
| 628 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 629 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 630 | Slog.w(TAG, "Bad device admin requested: " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 631 | return null; |
| 632 | } catch (IOException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 633 | Slog.w(TAG, "Bad device admin requested: " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 634 | return null; |
| 635 | } |
| 636 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 637 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 638 | private static JournaledFile makeJournaledFile() { |
| 639 | final String base = "/data/system/device_policies.xml"; |
| 640 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 641 | } |
| 642 | |
| 643 | private void saveSettingsLocked() { |
| 644 | JournaledFile journal = makeJournaledFile(); |
| 645 | FileOutputStream stream = null; |
| 646 | try { |
| 647 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 648 | XmlSerializer out = new FastXmlSerializer(); |
| 649 | out.setOutput(stream, "utf-8"); |
| 650 | out.startDocument(null, true); |
| 651 | |
| 652 | out.startTag(null, "policies"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 653 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 654 | final int N = mAdminList.size(); |
| 655 | for (int i=0; i<N; i++) { |
| 656 | ActiveAdmin ap = mAdminList.get(i); |
| 657 | if (ap != null) { |
| 658 | out.startTag(null, "admin"); |
| 659 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 660 | ap.writeToXml(out); |
| 661 | out.endTag(null, "admin"); |
| 662 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 663 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 664 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 665 | if (mPasswordOwner >= 0) { |
| 666 | out.startTag(null, "password-owner"); |
| 667 | out.attribute(null, "value", Integer.toString(mPasswordOwner)); |
| 668 | out.endTag(null, "password-owner"); |
| 669 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 670 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 671 | if (mFailedPasswordAttempts != 0) { |
| 672 | out.startTag(null, "failed-password-attempts"); |
| 673 | out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts)); |
| 674 | out.endTag(null, "failed-password-attempts"); |
| 675 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 676 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 677 | if (mActivePasswordQuality != 0 || mActivePasswordLength != 0 |
| 678 | || mActivePasswordUpperCase != 0 || mActivePasswordLowerCase != 0 |
| 679 | || mActivePasswordLetters != 0 || mActivePasswordNumeric != 0 |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 680 | || mActivePasswordSymbols != 0 || mActivePasswordNonLetter != 0) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 681 | out.startTag(null, "active-password"); |
| 682 | out.attribute(null, "quality", Integer.toString(mActivePasswordQuality)); |
| 683 | out.attribute(null, "length", Integer.toString(mActivePasswordLength)); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 684 | out.attribute(null, "uppercase", Integer.toString(mActivePasswordUpperCase)); |
| 685 | out.attribute(null, "lowercase", Integer.toString(mActivePasswordLowerCase)); |
| 686 | out.attribute(null, "letters", Integer.toString(mActivePasswordLetters)); |
| 687 | out.attribute(null, "numeric", Integer |
| 688 | .toString(mActivePasswordNumeric)); |
| 689 | out.attribute(null, "symbols", Integer.toString(mActivePasswordSymbols)); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 690 | out.attribute(null, "nonletter", Integer.toString(mActivePasswordNonLetter)); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 691 | out.endTag(null, "active-password"); |
| 692 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 693 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 694 | out.endTag(null, "policies"); |
| 695 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 696 | out.endDocument(); |
| 697 | stream.close(); |
| 698 | journal.commit(); |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 699 | sendChangedNotification(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 700 | } catch (IOException e) { |
| 701 | try { |
| 702 | if (stream != null) { |
| 703 | stream.close(); |
| 704 | } |
| 705 | } catch (IOException ex) { |
| 706 | // Ignore |
| 707 | } |
| 708 | journal.rollback(); |
| 709 | } |
| 710 | } |
| 711 | |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 712 | private void sendChangedNotification() { |
| 713 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 714 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 715 | mContext.sendBroadcast(intent); |
| 716 | } |
| 717 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 718 | private void loadSettingsLocked() { |
| 719 | JournaledFile journal = makeJournaledFile(); |
| 720 | FileInputStream stream = null; |
| 721 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 722 | try { |
| 723 | stream = new FileInputStream(file); |
| 724 | XmlPullParser parser = Xml.newPullParser(); |
| 725 | parser.setInput(stream, null); |
| 726 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 727 | int type; |
| 728 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 729 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 730 | } |
| 731 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 732 | if (!"policies".equals(tag)) { |
| 733 | throw new XmlPullParserException( |
| 734 | "Settings do not start with policies tag: found " + tag); |
| 735 | } |
| 736 | type = parser.next(); |
| 737 | int outerDepth = parser.getDepth(); |
| 738 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 739 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 740 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 741 | continue; |
| 742 | } |
| 743 | tag = parser.getName(); |
| 744 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 745 | String name = parser.getAttributeValue(null, "name"); |
| 746 | try { |
| 747 | DeviceAdminInfo dai = findAdmin( |
| 748 | ComponentName.unflattenFromString(name)); |
| 749 | if (dai != null) { |
| 750 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 751 | ap.readFromXml(parser); |
| 752 | mAdminMap.put(ap.info.getComponent(), ap); |
| 753 | mAdminList.add(ap); |
| 754 | } |
| 755 | } catch (RuntimeException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 756 | Slog.w(TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 757 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 758 | } else if ("failed-password-attempts".equals(tag)) { |
| 759 | mFailedPasswordAttempts = Integer.parseInt( |
| 760 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 761 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 762 | } else if ("password-owner".equals(tag)) { |
| 763 | mPasswordOwner = Integer.parseInt( |
| 764 | parser.getAttributeValue(null, "value")); |
| 765 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 766 | } else if ("active-password".equals(tag)) { |
| 767 | mActivePasswordQuality = Integer.parseInt( |
| 768 | parser.getAttributeValue(null, "quality")); |
| 769 | mActivePasswordLength = Integer.parseInt( |
| 770 | parser.getAttributeValue(null, "length")); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 771 | mActivePasswordUpperCase = Integer.parseInt( |
| 772 | parser.getAttributeValue(null, "uppercase")); |
| 773 | mActivePasswordLowerCase = Integer.parseInt( |
| 774 | parser.getAttributeValue(null, "lowercase")); |
| 775 | mActivePasswordLetters = Integer.parseInt( |
| 776 | parser.getAttributeValue(null, "letters")); |
| 777 | mActivePasswordNumeric = Integer.parseInt( |
| 778 | parser.getAttributeValue(null, "numeric")); |
| 779 | mActivePasswordSymbols = Integer.parseInt( |
| 780 | parser.getAttributeValue(null, "symbols")); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 781 | mActivePasswordNonLetter = Integer.parseInt( |
| 782 | parser.getAttributeValue(null, "nonletter")); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 783 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 784 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 785 | Slog.w(TAG, "Unknown tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 786 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 787 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 788 | } |
| 789 | } catch (NullPointerException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 790 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 791 | } catch (NumberFormatException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 792 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 793 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 794 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 795 | } catch (FileNotFoundException e) { |
| 796 | // Don't be noisy, this is normal if we haven't defined any policies. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 797 | } catch (IOException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 798 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 799 | } catch (IndexOutOfBoundsException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 800 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 801 | } |
| 802 | try { |
| 803 | if (stream != null) { |
| 804 | stream.close(); |
| 805 | } |
| 806 | } catch (IOException e) { |
| 807 | // Ignore |
| 808 | } |
| 809 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 810 | // Validate that what we stored for the password quality matches |
| 811 | // sufficiently what is currently set. Note that this is only |
| 812 | // a sanity check in case the two get out of sync; this should |
| 813 | // never normally happen. |
| 814 | LockPatternUtils utils = new LockPatternUtils(mContext); |
| 815 | if (utils.getActivePasswordQuality() < mActivePasswordQuality) { |
| 816 | Slog.w(TAG, "Active password quality 0x" |
| 817 | + Integer.toHexString(mActivePasswordQuality) |
| 818 | + " does not match actual quality 0x" |
| 819 | + Integer.toHexString(utils.getActivePasswordQuality())); |
| 820 | mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 821 | mActivePasswordLength = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 822 | mActivePasswordUpperCase = 0; |
| 823 | mActivePasswordLowerCase = 0; |
| 824 | mActivePasswordLetters = 0; |
| 825 | mActivePasswordNumeric = 0; |
| 826 | mActivePasswordSymbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 827 | mActivePasswordNonLetter = 0; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 828 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 829 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 830 | validatePasswordOwnerLocked(); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 831 | syncDeviceCapabilitiesLocked(); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 832 | updateMaximumTimeToLockLocked(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 833 | } |
| 834 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 835 | static void validateQualityConstant(int quality) { |
| 836 | switch (quality) { |
| 837 | case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: |
Danielle Millett | de7a2f3 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 838 | case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 839 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
| 840 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
| 841 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
| 842 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 843 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | throw new IllegalArgumentException("Invalid quality constant: 0x" |
| 847 | + Integer.toHexString(quality)); |
| 848 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 849 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 850 | void validatePasswordOwnerLocked() { |
| 851 | if (mPasswordOwner >= 0) { |
| 852 | boolean haveOwner = false; |
| 853 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 854 | if (mAdminList.get(i).getUid() == mPasswordOwner) { |
| 855 | haveOwner = true; |
| 856 | break; |
| 857 | } |
| 858 | } |
| 859 | if (!haveOwner) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 860 | Slog.w(TAG, "Previous password owner " + mPasswordOwner |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 861 | + " no longer active; disabling"); |
| 862 | mPasswordOwner = -1; |
| 863 | } |
| 864 | } |
| 865 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 866 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 867 | /** |
| 868 | * Pushes down policy information to the system for any policies related to general device |
| 869 | * capabilities that need to be enforced by lower level services (e.g. Camera services). |
| 870 | */ |
| 871 | void syncDeviceCapabilitiesLocked() { |
| 872 | // Ensure the status of the camera is synced down to the system. Interested native services |
| 873 | // should monitor this value and act accordingly. |
| 874 | boolean systemState = SystemProperties.getBoolean(SYSTEM_PROP_DISABLE_CAMERA, false); |
| 875 | boolean cameraDisabled = getCameraDisabled(null); |
| 876 | if (cameraDisabled != systemState) { |
| 877 | long token = Binder.clearCallingIdentity(); |
| 878 | try { |
| 879 | String value = cameraDisabled ? "1" : "0"; |
| 880 | Slog.v(TAG, "Change in camera state [" |
| 881 | + SYSTEM_PROP_DISABLE_CAMERA + "] = " + value); |
| 882 | SystemProperties.set(SYSTEM_PROP_DISABLE_CAMERA, value); |
| 883 | } finally { |
| 884 | Binder.restoreCallingIdentity(token); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 889 | public void systemReady() { |
| 890 | synchronized (this) { |
| 891 | loadSettingsLocked(); |
| 892 | } |
| 893 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 894 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 895 | private void handlePasswordExpirationNotification() { |
| 896 | synchronized (this) { |
| 897 | final long now = System.currentTimeMillis(); |
| 898 | final int N = mAdminList.size(); |
| 899 | if (N <= 0) { |
| 900 | return; |
| 901 | } |
| 902 | for (int i=0; i < N; i++) { |
| 903 | ActiveAdmin admin = mAdminList.get(i); |
| 904 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) |
| 905 | && admin.passwordExpirationTimeout > 0L |
| 906 | && admin.passwordExpirationDate > 0L |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 907 | && now >= admin.passwordExpirationDate - EXPIRATION_GRACE_PERIOD_MS) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 908 | sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING); |
| 909 | } |
| 910 | } |
| 911 | setExpirationAlarmCheckLocked(mContext); |
| 912 | } |
| 913 | } |
| 914 | |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 915 | /** |
| 916 | * @param adminReceiver The admin to add |
| 917 | * @param refreshing true = update an active admin, no error |
| 918 | */ |
| 919 | public void setActiveAdmin(ComponentName adminReceiver, boolean refreshing) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 920 | mContext.enforceCallingOrSelfPermission( |
| 921 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 922 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 923 | DeviceAdminInfo info = findAdmin(adminReceiver); |
| 924 | if (info == null) { |
| 925 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 926 | } |
| 927 | synchronized (this) { |
| 928 | long ident = Binder.clearCallingIdentity(); |
| 929 | try { |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 930 | if (!refreshing && getActiveAdminUncheckedLocked(adminReceiver) != null) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 931 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 932 | } |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 933 | ActiveAdmin newAdmin = new ActiveAdmin(info); |
| 934 | mAdminMap.put(adminReceiver, newAdmin); |
| 935 | int replaceIndex = -1; |
| 936 | if (refreshing) { |
| 937 | final int N = mAdminList.size(); |
| 938 | for (int i=0; i < N; i++) { |
| 939 | ActiveAdmin oldAdmin = mAdminList.get(i); |
| 940 | if (oldAdmin.info.getComponent().equals(adminReceiver)) { |
| 941 | replaceIndex = i; |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | if (replaceIndex == -1) { |
| 947 | mAdminList.add(newAdmin); |
| 948 | } else { |
| 949 | mAdminList.set(replaceIndex, newAdmin); |
| 950 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 951 | saveSettingsLocked(); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 952 | sendAdminCommandLocked(newAdmin, DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 953 | } finally { |
| 954 | Binder.restoreCallingIdentity(ident); |
| 955 | } |
| 956 | } |
| 957 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 958 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 959 | public boolean isAdminActive(ComponentName adminReceiver) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 960 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 961 | return getActiveAdminUncheckedLocked(adminReceiver) != null; |
| 962 | } |
| 963 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 964 | |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 965 | public boolean hasGrantedPolicy(ComponentName adminReceiver, int policyId) { |
| 966 | synchronized (this) { |
| 967 | ActiveAdmin administrator = getActiveAdminUncheckedLocked(adminReceiver); |
| 968 | if (administrator == null) { |
| 969 | throw new SecurityException("No active admin " + adminReceiver); |
| 970 | } |
| 971 | return administrator.info.usesPolicy(policyId); |
| 972 | } |
| 973 | } |
| 974 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 975 | public List<ComponentName> getActiveAdmins() { |
| 976 | synchronized (this) { |
| 977 | final int N = mAdminList.size(); |
| 978 | if (N <= 0) { |
| 979 | return null; |
| 980 | } |
| 981 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 982 | for (int i=0; i<N; i++) { |
| 983 | res.add(mAdminList.get(i).info.getComponent()); |
| 984 | } |
| 985 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 988 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 989 | public boolean packageHasActiveAdmins(String packageName) { |
| 990 | synchronized (this) { |
| 991 | final int N = mAdminList.size(); |
| 992 | for (int i=0; i<N; i++) { |
| 993 | if (mAdminList.get(i).info.getPackageName().equals(packageName)) { |
| 994 | return true; |
| 995 | } |
| 996 | } |
| 997 | return false; |
| 998 | } |
| 999 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1000 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1001 | public void removeActiveAdmin(ComponentName adminReceiver) { |
| 1002 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1003 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 1004 | if (admin == null) { |
| 1005 | return; |
| 1006 | } |
| 1007 | if (admin.getUid() != Binder.getCallingUid()) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1008 | mContext.enforceCallingOrSelfPermission( |
| 1009 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 1010 | } |
| 1011 | long ident = Binder.clearCallingIdentity(); |
| 1012 | try { |
| 1013 | removeActiveAdminLocked(adminReceiver); |
| 1014 | } finally { |
| 1015 | Binder.restoreCallingIdentity(ident); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1019 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1020 | public void setPasswordQuality(ComponentName who, int quality) { |
| 1021 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1022 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1023 | synchronized (this) { |
| 1024 | if (who == null) { |
| 1025 | throw new NullPointerException("ComponentName is null"); |
| 1026 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1027 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1028 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1029 | if (ap.passwordQuality != quality) { |
| 1030 | ap.passwordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1031 | saveSettingsLocked(); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1035 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1036 | public int getPasswordQuality(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1037 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1038 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1039 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1040 | if (who != null) { |
| 1041 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1042 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1043 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1044 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1045 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1046 | for (int i=0; i<N; i++) { |
| 1047 | ActiveAdmin admin = mAdminList.get(i); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1048 | if (mode < admin.passwordQuality) { |
| 1049 | mode = admin.passwordQuality; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1053 | } |
| 1054 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1055 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1056 | public void setPasswordMinimumLength(ComponentName who, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1057 | synchronized (this) { |
| 1058 | if (who == null) { |
| 1059 | throw new NullPointerException("ComponentName is null"); |
| 1060 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1061 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1062 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1063 | if (ap.minimumPasswordLength != length) { |
| 1064 | ap.minimumPasswordLength = length; |
| 1065 | saveSettingsLocked(); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1069 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1070 | public int getPasswordMinimumLength(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1071 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1072 | int length = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1073 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1074 | if (who != null) { |
| 1075 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1076 | return admin != null ? admin.minimumPasswordLength : length; |
| 1077 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1078 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1079 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1080 | for (int i=0; i<N; i++) { |
| 1081 | ActiveAdmin admin = mAdminList.get(i); |
| 1082 | if (length < admin.minimumPasswordLength) { |
| 1083 | length = admin.minimumPasswordLength; |
| 1084 | } |
| 1085 | } |
| 1086 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1087 | } |
| 1088 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1089 | |
| 1090 | public void setPasswordHistoryLength(ComponentName who, int length) { |
| 1091 | synchronized (this) { |
| 1092 | if (who == null) { |
| 1093 | throw new NullPointerException("ComponentName is null"); |
| 1094 | } |
| 1095 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1096 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1097 | if (ap.passwordHistoryLength != length) { |
| 1098 | ap.passwordHistoryLength = length; |
| 1099 | saveSettingsLocked(); |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | public int getPasswordHistoryLength(ComponentName who) { |
| 1105 | synchronized (this) { |
| 1106 | int length = 0; |
| 1107 | |
| 1108 | if (who != null) { |
| 1109 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1110 | return admin != null ? admin.passwordHistoryLength : length; |
| 1111 | } |
| 1112 | |
| 1113 | final int N = mAdminList.size(); |
| 1114 | for (int i = 0; i < N; i++) { |
| 1115 | ActiveAdmin admin = mAdminList.get(i); |
| 1116 | if (length < admin.passwordHistoryLength) { |
| 1117 | length = admin.passwordHistoryLength; |
| 1118 | } |
| 1119 | } |
| 1120 | return length; |
| 1121 | } |
| 1122 | } |
| 1123 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1124 | public void setPasswordExpirationTimeout(ComponentName who, long timeout) { |
| 1125 | synchronized (this) { |
| 1126 | if (who == null) { |
| 1127 | throw new NullPointerException("ComponentName is null"); |
| 1128 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1129 | if (timeout < 0) { |
| 1130 | throw new IllegalArgumentException("Timeout must be >= 0 ms"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1131 | } |
| 1132 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1133 | DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD); |
| 1134 | // Calling this API automatically bumps the expiration date |
| 1135 | final long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 1136 | ap.passwordExpirationDate = expiration; |
| 1137 | ap.passwordExpirationTimeout = timeout; |
| 1138 | if (timeout > 0L) { |
| 1139 | Slog.w(TAG, "setPasswordExpiration(): password will expire on " |
| 1140 | + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT) |
| 1141 | .format(new Date(expiration))); |
| 1142 | } |
| 1143 | saveSettingsLocked(); |
| 1144 | setExpirationAlarmCheckLocked(mContext); // in case this is the first one |
| 1145 | } |
| 1146 | } |
| 1147 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1148 | /** |
| 1149 | * Return a single admin's expiration cycle time, or the min of all cycle times. |
| 1150 | * Returns 0 if not configured. |
| 1151 | */ |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1152 | public long getPasswordExpirationTimeout(ComponentName who) { |
| 1153 | synchronized (this) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1154 | if (who != null) { |
| 1155 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1156 | return admin != null ? admin.passwordExpirationTimeout : 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1159 | long timeout = 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1160 | final int N = mAdminList.size(); |
| 1161 | for (int i = 0; i < N; i++) { |
| 1162 | ActiveAdmin admin = mAdminList.get(i); |
| 1163 | if (timeout == 0L || (admin.passwordExpirationTimeout != 0L |
| 1164 | && timeout > admin.passwordExpirationTimeout)) { |
| 1165 | timeout = admin.passwordExpirationTimeout; |
| 1166 | } |
| 1167 | } |
| 1168 | return timeout; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1172 | /** |
| 1173 | * Return a single admin's expiration date/time, or the min (soonest) for all admins. |
| 1174 | * Returns 0 if not configured. |
| 1175 | */ |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1176 | private long getPasswordExpirationLocked(ComponentName who) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1177 | if (who != null) { |
| 1178 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1179 | return admin != null ? admin.passwordExpirationDate : 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1182 | long timeout = 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1183 | final int N = mAdminList.size(); |
| 1184 | for (int i = 0; i < N; i++) { |
| 1185 | ActiveAdmin admin = mAdminList.get(i); |
| 1186 | if (timeout == 0L || (admin.passwordExpirationDate != 0 |
| 1187 | && timeout > admin.passwordExpirationDate)) { |
| 1188 | timeout = admin.passwordExpirationDate; |
| 1189 | } |
| 1190 | } |
| 1191 | return timeout; |
| 1192 | } |
| 1193 | |
| 1194 | public long getPasswordExpiration(ComponentName who) { |
| 1195 | synchronized (this) { |
| 1196 | return getPasswordExpirationLocked(who); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1200 | public void setPasswordMinimumUpperCase(ComponentName who, int length) { |
| 1201 | synchronized (this) { |
| 1202 | if (who == null) { |
| 1203 | throw new NullPointerException("ComponentName is null"); |
| 1204 | } |
| 1205 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1206 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1207 | if (ap.minimumPasswordUpperCase != length) { |
| 1208 | ap.minimumPasswordUpperCase = length; |
| 1209 | saveSettingsLocked(); |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | public int getPasswordMinimumUpperCase(ComponentName who) { |
| 1215 | synchronized (this) { |
| 1216 | int length = 0; |
| 1217 | |
| 1218 | if (who != null) { |
| 1219 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1220 | return admin != null ? admin.minimumPasswordUpperCase : length; |
| 1221 | } |
| 1222 | |
| 1223 | final int N = mAdminList.size(); |
| 1224 | for (int i=0; i<N; i++) { |
| 1225 | ActiveAdmin admin = mAdminList.get(i); |
| 1226 | if (length < admin.minimumPasswordUpperCase) { |
| 1227 | length = admin.minimumPasswordUpperCase; |
| 1228 | } |
| 1229 | } |
| 1230 | return length; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | public void setPasswordMinimumLowerCase(ComponentName who, int length) { |
| 1235 | synchronized (this) { |
| 1236 | if (who == null) { |
| 1237 | throw new NullPointerException("ComponentName is null"); |
| 1238 | } |
| 1239 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1240 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1241 | if (ap.minimumPasswordLowerCase != length) { |
| 1242 | ap.minimumPasswordLowerCase = length; |
| 1243 | saveSettingsLocked(); |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | public int getPasswordMinimumLowerCase(ComponentName who) { |
| 1249 | synchronized (this) { |
| 1250 | int length = 0; |
| 1251 | |
| 1252 | if (who != null) { |
| 1253 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1254 | return admin != null ? admin.minimumPasswordLowerCase : length; |
| 1255 | } |
| 1256 | |
| 1257 | final int N = mAdminList.size(); |
| 1258 | for (int i=0; i<N; i++) { |
| 1259 | ActiveAdmin admin = mAdminList.get(i); |
| 1260 | if (length < admin.minimumPasswordLowerCase) { |
| 1261 | length = admin.minimumPasswordLowerCase; |
| 1262 | } |
| 1263 | } |
| 1264 | return length; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | public void setPasswordMinimumLetters(ComponentName who, int length) { |
| 1269 | synchronized (this) { |
| 1270 | if (who == null) { |
| 1271 | throw new NullPointerException("ComponentName is null"); |
| 1272 | } |
| 1273 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1274 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1275 | if (ap.minimumPasswordLetters != length) { |
| 1276 | ap.minimumPasswordLetters = length; |
| 1277 | saveSettingsLocked(); |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | public int getPasswordMinimumLetters(ComponentName who) { |
| 1283 | synchronized (this) { |
| 1284 | int length = 0; |
| 1285 | |
| 1286 | if (who != null) { |
| 1287 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1288 | return admin != null ? admin.minimumPasswordLetters : length; |
| 1289 | } |
| 1290 | |
| 1291 | final int N = mAdminList.size(); |
| 1292 | for (int i=0; i<N; i++) { |
| 1293 | ActiveAdmin admin = mAdminList.get(i); |
| 1294 | if (length < admin.minimumPasswordLetters) { |
| 1295 | length = admin.minimumPasswordLetters; |
| 1296 | } |
| 1297 | } |
| 1298 | return length; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | public void setPasswordMinimumNumeric(ComponentName who, int length) { |
| 1303 | synchronized (this) { |
| 1304 | if (who == null) { |
| 1305 | throw new NullPointerException("ComponentName is null"); |
| 1306 | } |
| 1307 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1308 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1309 | if (ap.minimumPasswordNumeric != length) { |
| 1310 | ap.minimumPasswordNumeric = length; |
| 1311 | saveSettingsLocked(); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | public int getPasswordMinimumNumeric(ComponentName who) { |
| 1317 | synchronized (this) { |
| 1318 | int length = 0; |
| 1319 | |
| 1320 | if (who != null) { |
| 1321 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1322 | return admin != null ? admin.minimumPasswordNumeric : length; |
| 1323 | } |
| 1324 | |
| 1325 | final int N = mAdminList.size(); |
| 1326 | for (int i = 0; i < N; i++) { |
| 1327 | ActiveAdmin admin = mAdminList.get(i); |
| 1328 | if (length < admin.minimumPasswordNumeric) { |
| 1329 | length = admin.minimumPasswordNumeric; |
| 1330 | } |
| 1331 | } |
| 1332 | return length; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | public void setPasswordMinimumSymbols(ComponentName who, int length) { |
| 1337 | synchronized (this) { |
| 1338 | if (who == null) { |
| 1339 | throw new NullPointerException("ComponentName is null"); |
| 1340 | } |
| 1341 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1342 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1343 | if (ap.minimumPasswordSymbols != length) { |
| 1344 | ap.minimumPasswordSymbols = length; |
| 1345 | saveSettingsLocked(); |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | public int getPasswordMinimumSymbols(ComponentName who) { |
| 1351 | synchronized (this) { |
| 1352 | int length = 0; |
| 1353 | |
| 1354 | if (who != null) { |
| 1355 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1356 | return admin != null ? admin.minimumPasswordSymbols : length; |
| 1357 | } |
| 1358 | |
| 1359 | final int N = mAdminList.size(); |
| 1360 | for (int i=0; i<N; i++) { |
| 1361 | ActiveAdmin admin = mAdminList.get(i); |
| 1362 | if (length < admin.minimumPasswordSymbols) { |
| 1363 | length = admin.minimumPasswordSymbols; |
| 1364 | } |
| 1365 | } |
| 1366 | return length; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1370 | public void setPasswordMinimumNonLetter(ComponentName who, int length) { |
| 1371 | synchronized (this) { |
| 1372 | if (who == null) { |
| 1373 | throw new NullPointerException("ComponentName is null"); |
| 1374 | } |
| 1375 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1376 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1377 | if (ap.minimumPasswordNonLetter != length) { |
| 1378 | ap.minimumPasswordNonLetter = length; |
| 1379 | saveSettingsLocked(); |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | public int getPasswordMinimumNonLetter(ComponentName who) { |
| 1385 | synchronized (this) { |
| 1386 | int length = 0; |
| 1387 | |
| 1388 | if (who != null) { |
| 1389 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1390 | return admin != null ? admin.minimumPasswordNonLetter : length; |
| 1391 | } |
| 1392 | |
| 1393 | final int N = mAdminList.size(); |
| 1394 | for (int i=0; i<N; i++) { |
| 1395 | ActiveAdmin admin = mAdminList.get(i); |
| 1396 | if (length < admin.minimumPasswordNonLetter) { |
| 1397 | length = admin.minimumPasswordNonLetter; |
| 1398 | } |
| 1399 | } |
| 1400 | return length; |
| 1401 | } |
| 1402 | } |
| 1403 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1404 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1405 | synchronized (this) { |
| 1406 | // This API can only be called by an active device admin, |
| 1407 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1408 | getActiveAdminForCallerLocked(null, |
| 1409 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1410 | if (mActivePasswordQuality < getPasswordQuality(null) |
| 1411 | || mActivePasswordLength < getPasswordMinimumLength(null)) { |
| 1412 | return false; |
| 1413 | } |
| 1414 | if(mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 1415 | return true; |
| 1416 | } |
| 1417 | return mActivePasswordUpperCase >= getPasswordMinimumUpperCase(null) |
| 1418 | && mActivePasswordLowerCase >= getPasswordMinimumLowerCase(null) |
| 1419 | && mActivePasswordLetters >= getPasswordMinimumLetters(null) |
| 1420 | && mActivePasswordNumeric >= getPasswordMinimumNumeric(null) |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1421 | && mActivePasswordSymbols >= getPasswordMinimumSymbols(null) |
| 1422 | && mActivePasswordNonLetter >= getPasswordMinimumNonLetter(null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1423 | } |
| 1424 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1425 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1426 | public int getCurrentFailedPasswordAttempts() { |
| 1427 | synchronized (this) { |
| 1428 | // This API can only be called by an active device admin, |
| 1429 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1430 | getActiveAdminForCallerLocked(null, |
| 1431 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1432 | return mFailedPasswordAttempts; |
| 1433 | } |
| 1434 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1435 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1436 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) { |
| 1437 | synchronized (this) { |
| 1438 | // This API can only be called by an active device admin, |
| 1439 | // so try to retrieve it to check that the caller is one. |
| 1440 | getActiveAdminForCallerLocked(who, |
| 1441 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 1442 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1443 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 1444 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 1445 | ap.maximumFailedPasswordsForWipe = num; |
| 1446 | saveSettingsLocked(); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1450 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1451 | public int getMaximumFailedPasswordsForWipe(ComponentName who) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1452 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1453 | int count = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1454 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1455 | if (who != null) { |
| 1456 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1457 | return admin != null ? admin.maximumFailedPasswordsForWipe : count; |
| 1458 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1459 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1460 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1461 | for (int i=0; i<N; i++) { |
| 1462 | ActiveAdmin admin = mAdminList.get(i); |
| 1463 | if (count == 0) { |
| 1464 | count = admin.maximumFailedPasswordsForWipe; |
| 1465 | } else if (admin.maximumFailedPasswordsForWipe != 0 |
| 1466 | && count > admin.maximumFailedPasswordsForWipe) { |
| 1467 | count = admin.maximumFailedPasswordsForWipe; |
| 1468 | } |
| 1469 | } |
| 1470 | return count; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1471 | } |
| 1472 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1473 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1474 | public boolean resetPassword(String password, int flags) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1475 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1476 | synchronized (this) { |
| 1477 | // This API can only be called by an active device admin, |
| 1478 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1479 | getActiveAdminForCallerLocked(null, |
| 1480 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1481 | quality = getPasswordQuality(null); |
| 1482 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1483 | int realQuality = LockPatternUtils.computePasswordQuality(password); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1484 | if (realQuality < quality |
| 1485 | && quality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1486 | Slog.w(TAG, "resetPassword: password quality 0x" |
| 1487 | + Integer.toHexString(quality) |
| 1488 | + " does not meet required quality 0x" |
| 1489 | + Integer.toHexString(quality)); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1490 | return false; |
| 1491 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1492 | quality = Math.max(realQuality, quality); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1493 | } |
| 1494 | int length = getPasswordMinimumLength(null); |
| 1495 | if (password.length() < length) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1496 | Slog.w(TAG, "resetPassword: password length " + password.length() |
| 1497 | + " does not meet required length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1498 | return false; |
| 1499 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1500 | if (quality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 1501 | int letters = 0; |
| 1502 | int uppercase = 0; |
| 1503 | int lowercase = 0; |
| 1504 | int numbers = 0; |
| 1505 | int symbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1506 | int nonletter = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1507 | for (int i = 0; i < password.length(); i++) { |
| 1508 | char c = password.charAt(i); |
| 1509 | if (c >= 'A' && c <= 'Z') { |
| 1510 | letters++; |
| 1511 | uppercase++; |
| 1512 | } else if (c >= 'a' && c <= 'z') { |
| 1513 | letters++; |
| 1514 | lowercase++; |
| 1515 | } else if (c >= '0' && c <= '9') { |
| 1516 | numbers++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1517 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1518 | } else { |
| 1519 | symbols++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1520 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | int neededLetters = getPasswordMinimumLetters(null); |
| 1524 | if(letters < neededLetters) { |
| 1525 | Slog.w(TAG, "resetPassword: number of letters " + letters |
| 1526 | + " does not meet required number of letters " + neededLetters); |
| 1527 | return false; |
| 1528 | } |
| 1529 | int neededNumbers = getPasswordMinimumNumeric(null); |
| 1530 | if (numbers < neededNumbers) { |
| 1531 | Slog |
| 1532 | .w(TAG, "resetPassword: number of numerical digits " + numbers |
| 1533 | + " does not meet required number of numerical digits " |
| 1534 | + neededNumbers); |
| 1535 | return false; |
| 1536 | } |
| 1537 | int neededLowerCase = getPasswordMinimumLowerCase(null); |
| 1538 | if (lowercase < neededLowerCase) { |
| 1539 | Slog.w(TAG, "resetPassword: number of lowercase letters " + lowercase |
| 1540 | + " does not meet required number of lowercase letters " |
| 1541 | + neededLowerCase); |
| 1542 | return false; |
| 1543 | } |
| 1544 | int neededUpperCase = getPasswordMinimumUpperCase(null); |
| 1545 | if (uppercase < neededUpperCase) { |
| 1546 | Slog.w(TAG, "resetPassword: number of uppercase letters " + uppercase |
| 1547 | + " does not meet required number of uppercase letters " |
| 1548 | + neededUpperCase); |
| 1549 | return false; |
| 1550 | } |
| 1551 | int neededSymbols = getPasswordMinimumSymbols(null); |
| 1552 | if (symbols < neededSymbols) { |
| 1553 | Slog.w(TAG, "resetPassword: number of special symbols " + symbols |
| 1554 | + " does not meet required number of special symbols " + neededSymbols); |
| 1555 | return false; |
| 1556 | } |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1557 | int neededNonLetter = getPasswordMinimumNonLetter(null); |
| 1558 | if (nonletter < neededNonLetter) { |
| 1559 | Slog.w(TAG, "resetPassword: number of non-letter characters " + nonletter |
| 1560 | + " does not meet required number of non-letter characters " |
| 1561 | + neededNonLetter); |
| 1562 | return false; |
| 1563 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1564 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1565 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1566 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1567 | int callingUid = Binder.getCallingUid(); |
| 1568 | if (mPasswordOwner >= 0 && mPasswordOwner != callingUid) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1569 | 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] | 1570 | return false; |
| 1571 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1572 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1573 | // Don't do this with the lock held, because it is going to call |
| 1574 | // back in to the service. |
| 1575 | long ident = Binder.clearCallingIdentity(); |
| 1576 | try { |
| 1577 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1578 | utils.saveLockPassword(password, quality); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1579 | synchronized (this) { |
| 1580 | int newOwner = (flags&DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY) |
| 1581 | != 0 ? callingUid : -1; |
| 1582 | if (mPasswordOwner != newOwner) { |
| 1583 | mPasswordOwner = newOwner; |
| 1584 | saveSettingsLocked(); |
| 1585 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1586 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1587 | } finally { |
| 1588 | Binder.restoreCallingIdentity(ident); |
| 1589 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1590 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1591 | return true; |
| 1592 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1593 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1594 | public void setMaximumTimeToLock(ComponentName who, long timeMs) { |
| 1595 | synchronized (this) { |
| 1596 | if (who == null) { |
| 1597 | throw new NullPointerException("ComponentName is null"); |
| 1598 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1599 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 1600 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1601 | if (ap.maximumTimeToUnlock != timeMs) { |
| 1602 | ap.maximumTimeToUnlock = timeMs; |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1603 | saveSettingsLocked(); |
| 1604 | updateMaximumTimeToLockLocked(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1608 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1609 | void updateMaximumTimeToLockLocked() { |
| 1610 | long timeMs = getMaximumTimeToLock(null); |
| 1611 | if (mLastMaximumTimeToLock == timeMs) { |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | long ident = Binder.clearCallingIdentity(); |
| 1616 | try { |
| 1617 | if (timeMs <= 0) { |
| 1618 | timeMs = Integer.MAX_VALUE; |
| 1619 | } else { |
| 1620 | // Make sure KEEP_SCREEN_ON is disabled, since that |
| 1621 | // would allow bypassing of the maximum time to lock. |
| 1622 | Settings.System.putInt(mContext.getContentResolver(), |
| 1623 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0); |
| 1624 | } |
| 1625 | |
| 1626 | mLastMaximumTimeToLock = timeMs; |
| 1627 | |
| 1628 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1629 | getIPowerManager().setMaximumScreenOffTimeoutFromDeviceAdmin((int)timeMs); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1630 | } catch (RemoteException e) { |
| 1631 | Slog.w(TAG, "Failure talking with power manager", e); |
| 1632 | } |
| 1633 | } finally { |
| 1634 | Binder.restoreCallingIdentity(ident); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1638 | public long getMaximumTimeToLock(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1639 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1640 | long time = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1641 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1642 | if (who != null) { |
| 1643 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1644 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 1645 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1646 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1647 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1648 | for (int i=0; i<N; i++) { |
| 1649 | ActiveAdmin admin = mAdminList.get(i); |
| 1650 | if (time == 0) { |
| 1651 | time = admin.maximumTimeToUnlock; |
| 1652 | } else if (admin.maximumTimeToUnlock != 0 |
| 1653 | && time > admin.maximumTimeToUnlock) { |
| 1654 | time = admin.maximumTimeToUnlock; |
| 1655 | } |
| 1656 | } |
| 1657 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1658 | } |
| 1659 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1660 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1661 | public void lockNow() { |
| 1662 | synchronized (this) { |
| 1663 | // This API can only be called by an active device admin, |
| 1664 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1665 | getActiveAdminForCallerLocked(null, |
| 1666 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1667 | long ident = Binder.clearCallingIdentity(); |
| 1668 | try { |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 1669 | // Power off the display |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1670 | getIPowerManager().goToSleep(SystemClock.uptimeMillis(), |
| 1671 | PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN); |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 1672 | // Ensure the device is locked |
| 1673 | getWindowManager().lockNow(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1674 | } catch (RemoteException e) { |
| 1675 | } finally { |
| 1676 | Binder.restoreCallingIdentity(ident); |
| 1677 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1678 | } |
| 1679 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1680 | |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 1681 | private boolean isExtStorageEncrypted() { |
| 1682 | String state = SystemProperties.get("vold.decrypt"); |
| 1683 | return !"".equals(state); |
| 1684 | } |
| 1685 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1686 | void wipeDataLocked(int flags) { |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 1687 | // If the SD card is encrypted and non-removable, we have to force a wipe. |
| 1688 | boolean forceExtWipe = !Environment.isExternalStorageRemovable() && isExtStorageEncrypted(); |
| 1689 | boolean wipeExtRequested = (flags&DevicePolicyManager.WIPE_EXTERNAL_STORAGE) != 0; |
| 1690 | |
| 1691 | // Note: we can only do the wipe via ExternalStorageFormatter if the volume is not emulated. |
| 1692 | if ((forceExtWipe || wipeExtRequested) && !Environment.isExternalStorageEmulated()) { |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 1693 | Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET); |
Masanori Ogino | f535cb04 | 2012-02-15 19:25:50 +0900 | [diff] [blame] | 1694 | intent.putExtra(ExternalStorageFormatter.EXTRA_ALWAYS_RESET, true); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 1695 | intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); |
| 1696 | mWakeLock.acquire(10000); |
| 1697 | mContext.startService(intent); |
| 1698 | } else { |
| 1699 | try { |
| 1700 | RecoverySystem.rebootWipeUserData(mContext); |
| 1701 | } catch (IOException e) { |
| 1702 | Slog.w(TAG, "Failed requesting data wipe", e); |
| 1703 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1704 | } |
| 1705 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1706 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1707 | public void wipeData(int flags) { |
| 1708 | synchronized (this) { |
| 1709 | // This API can only be called by an active device admin, |
| 1710 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1711 | getActiveAdminForCallerLocked(null, |
| 1712 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1713 | long ident = Binder.clearCallingIdentity(); |
| 1714 | try { |
| 1715 | wipeDataLocked(flags); |
| 1716 | } finally { |
| 1717 | Binder.restoreCallingIdentity(ident); |
| 1718 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1719 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1720 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1721 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1722 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result) { |
| 1723 | mContext.enforceCallingOrSelfPermission( |
| 1724 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1725 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1726 | synchronized (this) { |
| 1727 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp); |
| 1728 | if (admin == null) { |
| 1729 | try { |
| 1730 | result.sendResult(null); |
| 1731 | } catch (RemoteException e) { |
| 1732 | } |
| 1733 | return; |
| 1734 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1735 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1736 | intent.setComponent(admin.info.getComponent()); |
| 1737 | mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { |
| 1738 | @Override |
| 1739 | public void onReceive(Context context, Intent intent) { |
| 1740 | try { |
| 1741 | result.sendResult(getResultExtras(false)); |
| 1742 | } catch (RemoteException e) { |
| 1743 | } |
| 1744 | } |
| 1745 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1746 | } |
| 1747 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1748 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1749 | public void setActivePasswordState(int quality, int length, int letters, int uppercase, |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1750 | int lowercase, int numbers, int symbols, int nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1751 | mContext.enforceCallingOrSelfPermission( |
| 1752 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1753 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1754 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1755 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1756 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1757 | if (mActivePasswordQuality != quality || mActivePasswordLength != length |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1758 | || mFailedPasswordAttempts != 0 || mActivePasswordLetters != letters |
| 1759 | || mActivePasswordUpperCase != uppercase |
| 1760 | || mActivePasswordLowerCase != lowercase || mActivePasswordNumeric != numbers |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1761 | || mActivePasswordSymbols != symbols || mActivePasswordNonLetter != nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1762 | long ident = Binder.clearCallingIdentity(); |
| 1763 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1764 | mActivePasswordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1765 | mActivePasswordLength = length; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1766 | mActivePasswordLetters = letters; |
| 1767 | mActivePasswordLowerCase = lowercase; |
| 1768 | mActivePasswordUpperCase = uppercase; |
| 1769 | mActivePasswordNumeric = numbers; |
| 1770 | mActivePasswordSymbols = symbols; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1771 | mActivePasswordNonLetter = nonletter; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1772 | mFailedPasswordAttempts = 0; |
| 1773 | saveSettingsLocked(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1774 | updatePasswordExpirationsLocked(); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1775 | setExpirationAlarmCheckLocked(mContext); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1776 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1777 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1778 | } finally { |
| 1779 | Binder.restoreCallingIdentity(ident); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1784 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1785 | /** |
| 1786 | * Called any time the device password is updated. Resets all password expiration clocks. |
| 1787 | */ |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1788 | private void updatePasswordExpirationsLocked() { |
| 1789 | final int N = mAdminList.size(); |
| 1790 | if (N > 0) { |
| 1791 | for (int i=0; i<N; i++) { |
| 1792 | ActiveAdmin admin = mAdminList.get(i); |
| 1793 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)) { |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1794 | long timeout = admin.passwordExpirationTimeout; |
| 1795 | long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 1796 | admin.passwordExpirationDate = expiration; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | saveSettingsLocked(); |
| 1800 | } |
| 1801 | } |
| 1802 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1803 | public void reportFailedPasswordAttempt() { |
| 1804 | mContext.enforceCallingOrSelfPermission( |
| 1805 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1806 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1807 | synchronized (this) { |
| 1808 | long ident = Binder.clearCallingIdentity(); |
| 1809 | try { |
| 1810 | mFailedPasswordAttempts++; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1811 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1812 | int max = getMaximumFailedPasswordsForWipe(null); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1813 | if (max > 0 && mFailedPasswordAttempts >= max) { |
| 1814 | wipeDataLocked(0); |
| 1815 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1816 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1817 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1818 | } finally { |
| 1819 | Binder.restoreCallingIdentity(ident); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1823 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1824 | public void reportSuccessfulPasswordAttempt() { |
| 1825 | mContext.enforceCallingOrSelfPermission( |
| 1826 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1827 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1828 | synchronized (this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1829 | if (mFailedPasswordAttempts != 0 || mPasswordOwner >= 0) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1830 | long ident = Binder.clearCallingIdentity(); |
| 1831 | try { |
| 1832 | mFailedPasswordAttempts = 0; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1833 | mPasswordOwner = -1; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1834 | saveSettingsLocked(); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1835 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1836 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1837 | } finally { |
| 1838 | Binder.restoreCallingIdentity(ident); |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1843 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1844 | public ComponentName setGlobalProxy(ComponentName who, String proxySpec, |
| 1845 | String exclusionList) { |
| 1846 | synchronized(this) { |
| 1847 | if (who == null) { |
| 1848 | throw new NullPointerException("ComponentName is null"); |
| 1849 | } |
| 1850 | |
| 1851 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 1852 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
| 1853 | |
| 1854 | // Scan through active admins and find if anyone has already |
| 1855 | // set the global proxy. |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1856 | Set<ComponentName> compSet = mAdminMap.keySet(); |
| 1857 | for (ComponentName component : compSet) { |
| 1858 | ActiveAdmin ap = mAdminMap.get(component); |
| 1859 | if ((ap.specifiesGlobalProxy) && (!component.equals(who))) { |
| 1860 | // Another admin already sets the global proxy |
| 1861 | // Return it to the caller. |
| 1862 | return component; |
| 1863 | } |
| 1864 | } |
| 1865 | if (proxySpec == null) { |
| 1866 | admin.specifiesGlobalProxy = false; |
| 1867 | admin.globalProxySpec = null; |
| 1868 | admin.globalProxyExclusionList = null; |
| 1869 | } else { |
| 1870 | |
| 1871 | admin.specifiesGlobalProxy = true; |
| 1872 | admin.globalProxySpec = proxySpec; |
| 1873 | admin.globalProxyExclusionList = exclusionList; |
| 1874 | } |
| 1875 | |
| 1876 | // Reset the global proxy accordingly |
| 1877 | // Do this using system permissions, as apps cannot write to secure settings |
| 1878 | long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1879 | resetGlobalProxyLocked(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1880 | Binder.restoreCallingIdentity(origId); |
| 1881 | return null; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | public ComponentName getGlobalProxyAdmin() { |
| 1886 | synchronized(this) { |
| 1887 | // Scan through active admins and find if anyone has already |
| 1888 | // set the global proxy. |
| 1889 | final int N = mAdminList.size(); |
| 1890 | for (int i = 0; i < N; i++) { |
| 1891 | ActiveAdmin ap = mAdminList.get(i); |
| 1892 | if (ap.specifiesGlobalProxy) { |
| 1893 | // Device admin sets the global proxy |
| 1894 | // Return it to the caller. |
| 1895 | return ap.info.getComponent(); |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | // No device admin sets the global proxy. |
| 1900 | return null; |
| 1901 | } |
| 1902 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1903 | private void resetGlobalProxyLocked() { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1904 | final int N = mAdminList.size(); |
| 1905 | for (int i = 0; i < N; i++) { |
| 1906 | ActiveAdmin ap = mAdminList.get(i); |
| 1907 | if (ap.specifiesGlobalProxy) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1908 | saveGlobalProxyLocked(ap.globalProxySpec, ap.globalProxyExclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | } |
| 1912 | // No device admins defining global proxies - reset global proxy settings to none |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1913 | saveGlobalProxyLocked(null, null); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1916 | private void saveGlobalProxyLocked(String proxySpec, String exclusionList) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1917 | if (exclusionList == null) { |
| 1918 | exclusionList = ""; |
| 1919 | } |
| 1920 | if (proxySpec == null) { |
| 1921 | proxySpec = ""; |
| 1922 | } |
| 1923 | // Remove white spaces |
| 1924 | proxySpec = proxySpec.trim(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1925 | String data[] = proxySpec.split(":"); |
| 1926 | int proxyPort = 8080; |
| 1927 | if (data.length > 1) { |
| 1928 | try { |
| 1929 | proxyPort = Integer.parseInt(data[1]); |
| 1930 | } catch (NumberFormatException e) {} |
| 1931 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1932 | exclusionList = exclusionList.trim(); |
| 1933 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1934 | Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, data[0]); |
| 1935 | Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, proxyPort); |
| 1936 | Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 1937 | exclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1940 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1941 | * Set the storage encryption request for a single admin. Returns the new total request |
| 1942 | * status (for all admins). |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1943 | */ |
| 1944 | public int setStorageEncryption(ComponentName who, boolean encrypt) { |
| 1945 | synchronized (this) { |
| 1946 | // Check for permissions |
| 1947 | if (who == null) { |
| 1948 | throw new NullPointerException("ComponentName is null"); |
| 1949 | } |
| 1950 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1951 | DeviceAdminInfo.USES_ENCRYPTED_STORAGE); |
| 1952 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1953 | // Quick exit: If the filesystem does not support encryption, we can exit early. |
| 1954 | if (!isEncryptionSupported()) { |
| 1955 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 1956 | } |
| 1957 | |
| 1958 | // (1) Record the value for the admin so it's sticky |
| 1959 | if (ap.encryptionRequested != encrypt) { |
| 1960 | ap.encryptionRequested = encrypt; |
| 1961 | saveSettingsLocked(); |
| 1962 | } |
| 1963 | |
| 1964 | // (2) Compute "max" for all admins |
| 1965 | boolean newRequested = false; |
| 1966 | final int N = mAdminList.size(); |
| 1967 | for (int i = 0; i < N; i++) { |
| 1968 | newRequested |= mAdminList.get(i).encryptionRequested; |
| 1969 | } |
| 1970 | |
| 1971 | // Notify OS of new request |
| 1972 | setEncryptionRequested(newRequested); |
| 1973 | |
| 1974 | // Return the new global request status |
| 1975 | return newRequested |
| 1976 | ? DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE |
| 1977 | : DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1982 | * Get the current storage encryption request status for a given admin, or aggregate of all |
| 1983 | * active admins. |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1984 | */ |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1985 | public boolean getStorageEncryption(ComponentName who) { |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1986 | synchronized (this) { |
| 1987 | // Check for permissions if a particular caller is specified |
| 1988 | if (who != null) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1989 | // When checking for a single caller, status is based on caller's request |
Andy Stadler | c994d69 | 2011-06-01 15:30:54 -0700 | [diff] [blame] | 1990 | ActiveAdmin ap = getActiveAdminUncheckedLocked(who); |
| 1991 | return ap != null ? ap.encryptionRequested : false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 1994 | // If no particular caller is specified, return the aggregate set of requests. |
| 1995 | // This is short circuited by returning true on the first hit. |
| 1996 | final int N = mAdminList.size(); |
| 1997 | for (int i = 0; i < N; i++) { |
| 1998 | if (mAdminList.get(i).encryptionRequested) { |
| 1999 | return true; |
| 2000 | } |
| 2001 | } |
| 2002 | return false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2006 | /** |
| 2007 | * Get the current encryption status of the device. |
| 2008 | */ |
| 2009 | public int getStorageEncryptionStatus() { |
| 2010 | return getEncryptionStatus(); |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * Hook to low-levels: This should report if the filesystem supports encrypted storage. |
| 2015 | */ |
| 2016 | private boolean isEncryptionSupported() { |
| 2017 | // Note, this can be implemented as |
| 2018 | // return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2019 | // But is provided as a separate internal method if there's a faster way to do a |
| 2020 | // simple check for supported-or-not. |
| 2021 | return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * Hook to low-levels: Reporting the current status of encryption. |
| 2026 | * @return A value such as {@link DevicePolicyManager#ENCRYPTION_STATUS_UNSUPPORTED} or |
| 2027 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_INACTIVE} or |
| 2028 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_ACTIVE}. |
| 2029 | */ |
| 2030 | private int getEncryptionStatus() { |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 2031 | String status = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 2032 | if ("encrypted".equalsIgnoreCase(status)) { |
| 2033 | return DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE; |
| 2034 | } else if ("unencrypted".equalsIgnoreCase(status)) { |
| 2035 | return DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
| 2036 | } else { |
| 2037 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2038 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | /** |
| 2042 | * Hook to low-levels: If needed, record the new admin setting for encryption. |
| 2043 | */ |
| 2044 | private void setEncryptionRequested(boolean encrypt) { |
| 2045 | } |
| 2046 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2047 | /** |
| 2048 | * The system property used to share the state of the camera. The native camera service |
| 2049 | * is expected to read this property and act accordingly. |
| 2050 | */ |
| 2051 | public static final String SYSTEM_PROP_DISABLE_CAMERA = "sys.secpolicy.camera.disabled"; |
| 2052 | |
| 2053 | /** |
| 2054 | * Disables all device cameras according to the specified admin. |
| 2055 | */ |
| 2056 | public void setCameraDisabled(ComponentName who, boolean disabled) { |
| 2057 | synchronized (this) { |
| 2058 | if (who == null) { |
| 2059 | throw new NullPointerException("ComponentName is null"); |
| 2060 | } |
| 2061 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2062 | DeviceAdminInfo.USES_POLICY_DISABLE_CAMERA); |
| 2063 | if (ap.disableCamera != disabled) { |
| 2064 | ap.disableCamera = disabled; |
| 2065 | saveSettingsLocked(); |
| 2066 | } |
| 2067 | syncDeviceCapabilitiesLocked(); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * Gets whether or not all device cameras are disabled for a given admin, or disabled for any |
| 2073 | * active admins. |
| 2074 | */ |
| 2075 | public boolean getCameraDisabled(ComponentName who) { |
| 2076 | synchronized (this) { |
| 2077 | if (who != null) { |
| 2078 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 2079 | return (admin != null) ? admin.disableCamera : false; |
| 2080 | } |
| 2081 | |
| 2082 | // Determine whether or not the device camera is disabled for any active admins. |
| 2083 | final int N = mAdminList.size(); |
| 2084 | for (int i = 0; i < N; i++) { |
| 2085 | ActiveAdmin admin = mAdminList.get(i); |
| 2086 | if (admin.disableCamera) { |
| 2087 | return true; |
| 2088 | } |
| 2089 | } |
| 2090 | return false; |
| 2091 | } |
| 2092 | } |
| 2093 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2094 | @Override |
| 2095 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2096 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2097 | != PackageManager.PERMISSION_GRANTED) { |
| 2098 | |
| 2099 | pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid=" |
| 2100 | + Binder.getCallingPid() |
| 2101 | + ", uid=" + Binder.getCallingUid()); |
| 2102 | return; |
| 2103 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2104 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2105 | final Printer p = new PrintWriterPrinter(pw); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2106 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2107 | synchronized (this) { |
| 2108 | p.println("Current Device Policy Manager state:"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2109 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2110 | p.println(" Enabled Device Admins:"); |
| 2111 | final int N = mAdminList.size(); |
| 2112 | for (int i=0; i<N; i++) { |
| 2113 | ActiveAdmin ap = mAdminList.get(i); |
| 2114 | if (ap != null) { |
| 2115 | pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString()); |
| 2116 | pw.println(":"); |
| 2117 | ap.dump(" ", pw); |
| 2118 | } |
| 2119 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2120 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2121 | pw.println(" "); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2122 | pw.print(" mPasswordOwner="); pw.println(mPasswordOwner); |
| 2123 | } |
| 2124 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2125 | } |