blob: e1312519480fc9eb31982611b89184d970600948 [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
17package com.android.server;
18
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;
25import android.os.Binder;
26import android.os.Process;
27import android.os.RemoteException;
28import android.os.ShellCommand;
29
30import com.android.internal.annotations.VisibleForTesting;
31import com.android.internal.widget.LockPatternUtils;
32import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
33
34class 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 Shankadc589ac2016-11-10 15:30:17 -080055 mCurrentUserId = ActivityManager.getService().getCurrentUser().id;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070056
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 Xu0cbc19e2016-12-09 14:00:21 +000080 getErrPrintWriter().println("Error while executing command: " + cmd);
81 e.printStackTrace(getErrPrintWriter());
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070082 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 {
119 mLockPatternUtils.clearLock(mCurrentUserId);
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}