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