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; |
| 25 | import android.os.Binder; |
| 26 | import android.os.Process; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ShellCommand; |
| 29 | |
| 30 | import com.android.internal.annotations.VisibleForTesting; |
| 31 | import com.android.internal.widget.LockPatternUtils; |
| 32 | import com.android.internal.widget.LockPatternUtils.RequestThrottledException; |
| 33 | |
| 34 | class LockSettingsShellCommand extends ShellCommand { |
| 35 | |
| 36 | private static final String COMMAND_SET_PATTERN = "set-pattern"; |
| 37 | private static final String COMMAND_SET_PIN = "set-pin"; |
| 38 | private static final String COMMAND_SET_PASSWORD = "set-password"; |
| 39 | private static final String COMMAND_CLEAR = "clear"; |
| 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; |
| 74 | default: |
| 75 | getErrPrintWriter().println("Unknown command: " + cmd); |
| 76 | break; |
| 77 | } |
| 78 | return 0; |
| 79 | } catch (Exception e) { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 80 | getErrPrintWriter().println("Error while executing command: " + cmd); |
| 81 | e.printStackTrace(getErrPrintWriter()); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onHelp() { |
| 88 | } |
| 89 | |
| 90 | private void parseArgs() { |
| 91 | String opt; |
| 92 | while ((opt = getNextOption()) != null) { |
| 93 | if ("--old".equals(opt)) { |
| 94 | mOld = getNextArgRequired(); |
| 95 | } else { |
| 96 | getErrPrintWriter().println("Unknown option: " + opt); |
| 97 | throw new IllegalArgumentException(); |
| 98 | } |
| 99 | } |
| 100 | mNew = getNextArg(); |
| 101 | } |
| 102 | |
| 103 | private void runSetPattern() throws RemoteException { |
| 104 | mLockPatternUtils.saveLockPattern(stringToPattern(mNew), mOld, mCurrentUserId); |
| 105 | getOutPrintWriter().println("Pattern set to '" + mNew + "'"); |
| 106 | } |
| 107 | |
| 108 | private void runSetPassword() throws RemoteException { |
| 109 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_ALPHABETIC, mCurrentUserId); |
| 110 | getOutPrintWriter().println("Password set to '" + mNew + "'"); |
| 111 | } |
| 112 | |
| 113 | private void runSetPin() throws RemoteException { |
| 114 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_NUMERIC, mCurrentUserId); |
| 115 | getOutPrintWriter().println("Pin set to '" + mNew + "'"); |
| 116 | } |
| 117 | |
| 118 | private void runClear() throws RemoteException { |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame^] | 119 | mLockPatternUtils.clearLock(mOld, mCurrentUserId); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 120 | getOutPrintWriter().println("Lock credential cleared"); |
| 121 | } |
| 122 | |
| 123 | private boolean checkCredential() throws RemoteException, RequestThrottledException { |
| 124 | final boolean havePassword = mLockPatternUtils.isLockPasswordEnabled(mCurrentUserId); |
| 125 | final boolean havePattern = mLockPatternUtils.isLockPatternEnabled(mCurrentUserId); |
| 126 | if (havePassword || havePattern) { |
| 127 | boolean result; |
| 128 | if (havePassword) { |
| 129 | result = mLockPatternUtils.checkPassword(mOld, mCurrentUserId); |
| 130 | } else { |
| 131 | result = mLockPatternUtils.checkPattern(stringToPattern(mOld), |
| 132 | mCurrentUserId); |
| 133 | } |
| 134 | if (result) { |
| 135 | return true; |
| 136 | } else { |
| 137 | getOutPrintWriter().println("Old password '" + mOld + "' didn't match"); |
| 138 | return false; |
| 139 | } |
| 140 | } else { |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | } |