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 | |
| 23 | import android.app.ActivityManagerNative; |
| 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 { |
| 55 | mCurrentUserId = ActivityManagerNative.getDefault().getCurrentUser().id; |
| 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) { |
| 80 | getErrPrintWriter().println("Error while executing command: " + e); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onHelp() { |
| 87 | } |
| 88 | |
| 89 | private void parseArgs() { |
| 90 | String opt; |
| 91 | while ((opt = getNextOption()) != null) { |
| 92 | if ("--old".equals(opt)) { |
| 93 | mOld = getNextArgRequired(); |
| 94 | } else { |
| 95 | getErrPrintWriter().println("Unknown option: " + opt); |
| 96 | throw new IllegalArgumentException(); |
| 97 | } |
| 98 | } |
| 99 | mNew = getNextArg(); |
| 100 | } |
| 101 | |
| 102 | private void runSetPattern() throws RemoteException { |
| 103 | mLockPatternUtils.saveLockPattern(stringToPattern(mNew), mOld, mCurrentUserId); |
| 104 | getOutPrintWriter().println("Pattern set to '" + mNew + "'"); |
| 105 | } |
| 106 | |
| 107 | private void runSetPassword() throws RemoteException { |
| 108 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_ALPHABETIC, mCurrentUserId); |
| 109 | getOutPrintWriter().println("Password set to '" + mNew + "'"); |
| 110 | } |
| 111 | |
| 112 | private void runSetPin() throws RemoteException { |
| 113 | mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_NUMERIC, mCurrentUserId); |
| 114 | getOutPrintWriter().println("Pin set to '" + mNew + "'"); |
| 115 | } |
| 116 | |
| 117 | private void runClear() throws RemoteException { |
| 118 | mLockPatternUtils.clearLock(mCurrentUserId); |
| 119 | getOutPrintWriter().println("Lock credential cleared"); |
| 120 | } |
| 121 | |
| 122 | private boolean checkCredential() throws RemoteException, RequestThrottledException { |
| 123 | final boolean havePassword = mLockPatternUtils.isLockPasswordEnabled(mCurrentUserId); |
| 124 | final boolean havePattern = mLockPatternUtils.isLockPatternEnabled(mCurrentUserId); |
| 125 | if (havePassword || havePattern) { |
| 126 | boolean result; |
| 127 | if (havePassword) { |
| 128 | result = mLockPatternUtils.checkPassword(mOld, mCurrentUserId); |
| 129 | } else { |
| 130 | result = mLockPatternUtils.checkPattern(stringToPattern(mOld), |
| 131 | mCurrentUserId); |
| 132 | } |
| 133 | if (result) { |
| 134 | return true; |
| 135 | } else { |
| 136 | getOutPrintWriter().println("Old password '" + mOld + "' didn't match"); |
| 137 | return false; |
| 138 | } |
| 139 | } else { |
| 140 | return true; |
| 141 | } |
| 142 | } |
| 143 | } |