blob: 4d2cf321b5ee76352ff7792dca5fa008a59e53d9 [file] [log] [blame]
Jorim Jaggi2fef6f72016-11-01 19:06:25 -07001/*
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 Scull507d11c2017-05-03 17:19:01 +010017package com.android.server.locksettings;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070018
19import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
20import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
21import static com.android.internal.widget.LockPatternUtils.stringToPattern;
22
Sudheer Shankadc589ac2016-11-10 15:30:17 -080023import android.app.ActivityManager;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070024import android.content.Context;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070025import android.os.RemoteException;
26import android.os.ShellCommand;
27
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070028import com.android.internal.widget.LockPatternUtils;
29import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
30
31class 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 Xu3bf722a2016-12-15 16:07:38 +000037 private static final String COMMAND_SP = "sp";
Jeff Sharkey65440992017-03-31 09:45:46 -060038 private static final String COMMAND_SET_DISABLED = "set-disabled";
Pavel Grafov5f679b22017-06-26 18:39:10 +010039 private static final String COMMAND_VERIFY = "verify";
chaviw5953e662017-08-21 10:46:11 -070040 private static final String COMMAND_GET_DISABLED = "get-disabled";
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070041
42 private int mCurrentUserId;
43 private final LockPatternUtils mLockPatternUtils;
44 private final Context mContext;
45 private String mOld = "";
46 private String mNew = "";
47
48 LockSettingsShellCommand(Context context, LockPatternUtils lockPatternUtils) {
49 mContext = context;
50 mLockPatternUtils = lockPatternUtils;
51 }
52
53 @Override
54 public int onCommand(String cmd) {
55 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -080056 mCurrentUserId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070057
58 parseArgs();
59 if (!checkCredential()) {
60 return -1;
61 }
62 switch (cmd) {
63 case COMMAND_SET_PATTERN:
64 runSetPattern();
65 break;
66 case COMMAND_SET_PASSWORD:
67 runSetPassword();
68 break;
69 case COMMAND_SET_PIN:
70 runSetPin();
71 break;
72 case COMMAND_CLEAR:
73 runClear();
74 break;
Rubin Xu3bf722a2016-12-15 16:07:38 +000075 case COMMAND_SP:
Paul Crowley7a0cc0a2017-05-31 22:12:57 +000076 runChangeSp();
Rubin Xu3bf722a2016-12-15 16:07:38 +000077 break;
Jeff Sharkey65440992017-03-31 09:45:46 -060078 case COMMAND_SET_DISABLED:
79 runSetDisabled();
80 break;
Pavel Grafov5f679b22017-06-26 18:39:10 +010081 case COMMAND_VERIFY:
82 runVerify();
83 break;
chaviw5953e662017-08-21 10:46:11 -070084 case COMMAND_GET_DISABLED:
85 runGetDisabled();
86 break;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070087 default:
88 getErrPrintWriter().println("Unknown command: " + cmd);
89 break;
90 }
91 return 0;
92 } catch (Exception e) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000093 getErrPrintWriter().println("Error while executing command: " + cmd);
94 e.printStackTrace(getErrPrintWriter());
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070095 return -1;
96 }
97 }
98
Pavel Grafov5f679b22017-06-26 18:39:10 +010099 private void runVerify() {
100 // The command is only run if the credential is correct.
101 getOutPrintWriter().println("Lock credential verified successfully");
102 }
103
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700104 @Override
105 public void onHelp() {
106 }
107
108 private void parseArgs() {
109 String opt;
110 while ((opt = getNextOption()) != null) {
111 if ("--old".equals(opt)) {
112 mOld = getNextArgRequired();
Rubin Xu3bf722a2016-12-15 16:07:38 +0000113 } else if ("--user".equals(opt)) {
114 mCurrentUserId = Integer.parseInt(getNextArgRequired());
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700115 } else {
116 getErrPrintWriter().println("Unknown option: " + opt);
117 throw new IllegalArgumentException();
118 }
119 }
120 mNew = getNextArg();
121 }
122
Paul Crowley7a0cc0a2017-05-31 22:12:57 +0000123 private void runChangeSp() {
124 if (mNew != null ) {
125 if ("1".equals(mNew)) {
126 mLockPatternUtils.enableSyntheticPassword();
127 getOutPrintWriter().println("Synthetic password enabled");
128 } else if ("0".equals(mNew)) {
129 mLockPatternUtils.disableSyntheticPassword();
130 getOutPrintWriter().println("Synthetic password disabled");
131 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000132 }
133 getOutPrintWriter().println(String.format("SP Enabled = %b",
134 mLockPatternUtils.isSyntheticPasswordEnabled()));
135 }
136
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700137 private void runSetPattern() throws RemoteException {
138 mLockPatternUtils.saveLockPattern(stringToPattern(mNew), mOld, mCurrentUserId);
139 getOutPrintWriter().println("Pattern set to '" + mNew + "'");
140 }
141
142 private void runSetPassword() throws RemoteException {
143 mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_ALPHABETIC, mCurrentUserId);
144 getOutPrintWriter().println("Password set to '" + mNew + "'");
145 }
146
147 private void runSetPin() throws RemoteException {
148 mLockPatternUtils.saveLockPassword(mNew, mOld, PASSWORD_QUALITY_NUMERIC, mCurrentUserId);
149 getOutPrintWriter().println("Pin set to '" + mNew + "'");
150 }
151
152 private void runClear() throws RemoteException {
Rubin Xua55b1682017-01-31 10:06:56 +0000153 mLockPatternUtils.clearLock(mOld, mCurrentUserId);
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700154 getOutPrintWriter().println("Lock credential cleared");
155 }
156
Jeff Sharkey65440992017-03-31 09:45:46 -0600157 private void runSetDisabled() throws RemoteException {
158 final boolean disabled = Boolean.parseBoolean(mNew);
159 mLockPatternUtils.setLockScreenDisabled(disabled, mCurrentUserId);
160 getOutPrintWriter().println("Lock screen disabled set to " + disabled);
161 }
162
chaviw5953e662017-08-21 10:46:11 -0700163 private void runGetDisabled() {
164 boolean isLockScreenDisabled = mLockPatternUtils.isLockScreenDisabled(mCurrentUserId);
165 getOutPrintWriter().println(isLockScreenDisabled);
166 }
167
Pavel Grafovc07067d2017-07-05 16:30:04 +0100168 private boolean checkCredential() throws RemoteException {
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700169 final boolean havePassword = mLockPatternUtils.isLockPasswordEnabled(mCurrentUserId);
170 final boolean havePattern = mLockPatternUtils.isLockPatternEnabled(mCurrentUserId);
171 if (havePassword || havePattern) {
Pavel Grafovfc135c72017-07-25 16:38:04 +0100172 if (mLockPatternUtils.isManagedProfileWithUnifiedChallenge(mCurrentUserId)) {
173 getOutPrintWriter().println("Profile uses unified challenge");
174 return false;
175 }
176
Pavel Grafovc07067d2017-07-05 16:30:04 +0100177 try {
178 final boolean result;
179 if (havePassword) {
180 result = mLockPatternUtils.checkPassword(mOld, mCurrentUserId);
181 } else {
182 result = mLockPatternUtils.checkPattern(stringToPattern(mOld), mCurrentUserId);
183 }
184 if (!result) {
Pavel Grafovb3191252017-07-14 12:30:31 +0100185 if (!mLockPatternUtils.isManagedProfileWithUnifiedChallenge(mCurrentUserId)) {
186 mLockPatternUtils.reportFailedPasswordAttempt(mCurrentUserId);
187 }
Pavel Grafovc07067d2017-07-05 16:30:04 +0100188 getOutPrintWriter().println("Old password '" + mOld + "' didn't match");
189 }
190 return result;
191 } catch (RequestThrottledException e) {
192 getOutPrintWriter().println("Request throttled");
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700193 return false;
194 }
195 } else {
196 return true;
197 }
198 }
199}