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 | |
| 17 | package com.android.server; |
| 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: |
| 74 | runEnableSp(); |
| 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 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 110 | private void runEnableSp() { |
| 111 | if (mNew != null) { |
| 112 | mLockPatternUtils.enableSyntheticPassword(); |
| 113 | getOutPrintWriter().println("Synthetic password enabled"); |
| 114 | } |
| 115 | getOutPrintWriter().println(String.format("SP Enabled = %b", |
| 116 | mLockPatternUtils.isSyntheticPasswordEnabled())); |
| 117 | } |
| 118 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 119 | private void runSetPattern() throws RemoteException { |
| 120 | mLockPatternUtils.saveLockPattern(stringToPattern(mNew), mOld, mCurrentUserId); |
| 121 | getOutPrintWriter().println("Pattern set to '" + mNew + "'"); |
| 122 | } |
| 123 | |
| 124 | private void runSetPassword() throws RemoteException { |
| 125 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_ALPHABETIC, mCurrentUserId); |
| 126 | getOutPrintWriter().println("Password set to '" + mNew + "'"); |
| 127 | } |
| 128 | |
| 129 | private void runSetPin() throws RemoteException { |
| 130 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_NUMERIC, mCurrentUserId); |
| 131 | getOutPrintWriter().println("Pin set to '" + mNew + "'"); |
| 132 | } |
| 133 | |
| 134 | private void runClear() throws RemoteException { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 135 | mLockPatternUtils.clearLock(mOld, mCurrentUserId); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 136 | getOutPrintWriter().println("Lock credential cleared"); |
| 137 | } |
| 138 | |
Jeff Sharkey | 6544099 | 2017-03-31 09:45:46 -0600 | [diff] [blame^] | 139 | private void runSetDisabled() throws RemoteException { |
| 140 | final boolean disabled = Boolean.parseBoolean(mNew); |
| 141 | mLockPatternUtils.setLockScreenDisabled(disabled, mCurrentUserId); |
| 142 | getOutPrintWriter().println("Lock screen disabled set to " + disabled); |
| 143 | } |
| 144 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 145 | private boolean checkCredential() throws RemoteException, RequestThrottledException { |
| 146 | final boolean havePassword = mLockPatternUtils.isLockPasswordEnabled(mCurrentUserId); |
| 147 | final boolean havePattern = mLockPatternUtils.isLockPatternEnabled(mCurrentUserId); |
| 148 | if (havePassword || havePattern) { |
| 149 | boolean result; |
| 150 | if (havePassword) { |
| 151 | result = mLockPatternUtils.checkPassword(mOld, mCurrentUserId); |
| 152 | } else { |
| 153 | result = mLockPatternUtils.checkPattern(stringToPattern(mOld), |
| 154 | mCurrentUserId); |
| 155 | } |
| 156 | if (result) { |
| 157 | return true; |
| 158 | } else { |
| 159 | getOutPrintWriter().println("Old password '" + mOld + "' didn't match"); |
| 160 | return false; |
| 161 | } |
| 162 | } else { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | } |