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