Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 17 | package com.android.server.locksettings; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 18 | |
| 19 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
| 20 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 21 | import static com.android.internal.widget.LockPatternUtils.stringToPattern; |
| 22 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.os.ShellCommand; |
| 27 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 28 | import com.android.internal.widget.LockPatternUtils; |
| 29 | import com.android.internal.widget.LockPatternUtils.RequestThrottledException; |
| 30 | |
| 31 | class LockSettingsShellCommand extends ShellCommand { |
| 32 | |
| 33 | private static final String COMMAND_SET_PATTERN = "set-pattern"; |
| 34 | private static final String COMMAND_SET_PIN = "set-pin"; |
| 35 | private static final String COMMAND_SET_PASSWORD = "set-password"; |
| 36 | private static final String COMMAND_CLEAR = "clear"; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 37 | private static final String COMMAND_SP = "sp"; |
Jeff Sharkey | 6544099 | 2017-03-31 09:45:46 -0600 | [diff] [blame] | 38 | private static final String COMMAND_SET_DISABLED = "set-disabled"; |
Pavel Grafov | 5f679b2 | 2017-06-26 18:39:10 +0100 | [diff] [blame] | 39 | private static final String COMMAND_VERIFY = "verify"; |
chaviw | 5953e66 | 2017-08-21 10:46:11 -0700 | [diff] [blame^] | 40 | private static final String COMMAND_GET_DISABLED = "get-disabled"; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 41 | |
| 42 | private int mCurrentUserId; |
| 43 | private final LockPatternUtils mLockPatternUtils; |
| 44 | private final Context mContext; |
| 45 | private String mOld = ""; |
| 46 | private String mNew = ""; |
| 47 | |
| 48 | LockSettingsShellCommand(Context context, LockPatternUtils lockPatternUtils) { |
| 49 | mContext = context; |
| 50 | mLockPatternUtils = lockPatternUtils; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int onCommand(String cmd) { |
| 55 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 56 | mCurrentUserId = ActivityManager.getService().getCurrentUser().id; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 57 | |
| 58 | parseArgs(); |
| 59 | if (!checkCredential()) { |
| 60 | return -1; |
| 61 | } |
| 62 | switch (cmd) { |
| 63 | case COMMAND_SET_PATTERN: |
| 64 | runSetPattern(); |
| 65 | break; |
| 66 | case COMMAND_SET_PASSWORD: |
| 67 | runSetPassword(); |
| 68 | break; |
| 69 | case COMMAND_SET_PIN: |
| 70 | runSetPin(); |
| 71 | break; |
| 72 | case COMMAND_CLEAR: |
| 73 | runClear(); |
| 74 | break; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 75 | case COMMAND_SP: |
Paul Crowley | 7a0cc0a | 2017-05-31 22:12:57 +0000 | [diff] [blame] | 76 | runChangeSp(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 77 | break; |
Jeff Sharkey | 6544099 | 2017-03-31 09:45:46 -0600 | [diff] [blame] | 78 | case COMMAND_SET_DISABLED: |
| 79 | runSetDisabled(); |
| 80 | break; |
Pavel Grafov | 5f679b2 | 2017-06-26 18:39:10 +0100 | [diff] [blame] | 81 | case COMMAND_VERIFY: |
| 82 | runVerify(); |
| 83 | break; |
chaviw | 5953e66 | 2017-08-21 10:46:11 -0700 | [diff] [blame^] | 84 | case COMMAND_GET_DISABLED: |
| 85 | runGetDisabled(); |
| 86 | break; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 87 | default: |
| 88 | getErrPrintWriter().println("Unknown command: " + cmd); |
| 89 | break; |
| 90 | } |
| 91 | return 0; |
| 92 | } catch (Exception e) { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 93 | getErrPrintWriter().println("Error while executing command: " + cmd); |
| 94 | e.printStackTrace(getErrPrintWriter()); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | } |
| 98 | |
Pavel Grafov | 5f679b2 | 2017-06-26 18:39:10 +0100 | [diff] [blame] | 99 | private void runVerify() { |
| 100 | // The command is only run if the credential is correct. |
| 101 | getOutPrintWriter().println("Lock credential verified successfully"); |
| 102 | } |
| 103 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public void onHelp() { |
| 106 | } |
| 107 | |
| 108 | private void parseArgs() { |
| 109 | String opt; |
| 110 | while ((opt = getNextOption()) != null) { |
| 111 | if ("--old".equals(opt)) { |
| 112 | mOld = getNextArgRequired(); |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 113 | } else if ("--user".equals(opt)) { |
| 114 | mCurrentUserId = Integer.parseInt(getNextArgRequired()); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 115 | } else { |
| 116 | getErrPrintWriter().println("Unknown option: " + opt); |
| 117 | throw new IllegalArgumentException(); |
| 118 | } |
| 119 | } |
| 120 | mNew = getNextArg(); |
| 121 | } |
| 122 | |
Paul Crowley | 7a0cc0a | 2017-05-31 22:12:57 +0000 | [diff] [blame] | 123 | private void runChangeSp() { |
| 124 | if (mNew != null ) { |
| 125 | if ("1".equals(mNew)) { |
| 126 | mLockPatternUtils.enableSyntheticPassword(); |
| 127 | getOutPrintWriter().println("Synthetic password enabled"); |
| 128 | } else if ("0".equals(mNew)) { |
| 129 | mLockPatternUtils.disableSyntheticPassword(); |
| 130 | getOutPrintWriter().println("Synthetic password disabled"); |
| 131 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 132 | } |
| 133 | getOutPrintWriter().println(String.format("SP Enabled = %b", |
| 134 | mLockPatternUtils.isSyntheticPasswordEnabled())); |
| 135 | } |
| 136 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 137 | private void runSetPattern() throws RemoteException { |
| 138 | mLockPatternUtils.saveLockPattern(stringToPattern(mNew), mOld, mCurrentUserId); |
| 139 | getOutPrintWriter().println("Pattern set to '" + mNew + "'"); |
| 140 | } |
| 141 | |
| 142 | private void runSetPassword() throws RemoteException { |
| 143 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_ALPHABETIC, mCurrentUserId); |
| 144 | getOutPrintWriter().println("Password set to '" + mNew + "'"); |
| 145 | } |
| 146 | |
| 147 | private void runSetPin() throws RemoteException { |
| 148 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_NUMERIC, mCurrentUserId); |
| 149 | getOutPrintWriter().println("Pin set to '" + mNew + "'"); |
| 150 | } |
| 151 | |
| 152 | private void runClear() throws RemoteException { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 153 | mLockPatternUtils.clearLock(mOld, mCurrentUserId); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 154 | getOutPrintWriter().println("Lock credential cleared"); |
| 155 | } |
| 156 | |
Jeff Sharkey | 6544099 | 2017-03-31 09:45:46 -0600 | [diff] [blame] | 157 | private void runSetDisabled() throws RemoteException { |
| 158 | final boolean disabled = Boolean.parseBoolean(mNew); |
| 159 | mLockPatternUtils.setLockScreenDisabled(disabled, mCurrentUserId); |
| 160 | getOutPrintWriter().println("Lock screen disabled set to " + disabled); |
| 161 | } |
| 162 | |
chaviw | 5953e66 | 2017-08-21 10:46:11 -0700 | [diff] [blame^] | 163 | private void runGetDisabled() { |
| 164 | boolean isLockScreenDisabled = mLockPatternUtils.isLockScreenDisabled(mCurrentUserId); |
| 165 | getOutPrintWriter().println(isLockScreenDisabled); |
| 166 | } |
| 167 | |
Pavel Grafov | c07067d | 2017-07-05 16:30:04 +0100 | [diff] [blame] | 168 | private boolean checkCredential() throws RemoteException { |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 169 | final boolean havePassword = mLockPatternUtils.isLockPasswordEnabled(mCurrentUserId); |
| 170 | final boolean havePattern = mLockPatternUtils.isLockPatternEnabled(mCurrentUserId); |
| 171 | if (havePassword || havePattern) { |
Pavel Grafov | fc135c7 | 2017-07-25 16:38:04 +0100 | [diff] [blame] | 172 | if (mLockPatternUtils.isManagedProfileWithUnifiedChallenge(mCurrentUserId)) { |
| 173 | getOutPrintWriter().println("Profile uses unified challenge"); |
| 174 | return false; |
| 175 | } |
| 176 | |
Pavel Grafov | c07067d | 2017-07-05 16:30:04 +0100 | [diff] [blame] | 177 | try { |
| 178 | final boolean result; |
| 179 | if (havePassword) { |
| 180 | result = mLockPatternUtils.checkPassword(mOld, mCurrentUserId); |
| 181 | } else { |
| 182 | result = mLockPatternUtils.checkPattern(stringToPattern(mOld), mCurrentUserId); |
| 183 | } |
| 184 | if (!result) { |
Pavel Grafov | b319125 | 2017-07-14 12:30:31 +0100 | [diff] [blame] | 185 | if (!mLockPatternUtils.isManagedProfileWithUnifiedChallenge(mCurrentUserId)) { |
| 186 | mLockPatternUtils.reportFailedPasswordAttempt(mCurrentUserId); |
| 187 | } |
Pavel Grafov | c07067d | 2017-07-05 16:30:04 +0100 | [diff] [blame] | 188 | getOutPrintWriter().println("Old password '" + mOld + "' didn't match"); |
| 189 | } |
| 190 | return result; |
| 191 | } catch (RequestThrottledException e) { |
| 192 | getOutPrintWriter().println("Request throttled"); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | } else { |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | } |