1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.util.shell.commands;
18
19 import java.io.IOException;
20
21 import org.apache.accumulo.core.client.AccumuloException;
22 import org.apache.accumulo.core.client.AccumuloSecurityException;
23 import org.apache.accumulo.core.security.CredentialHelper;
24 import org.apache.accumulo.core.security.thrift.SecurityErrorCode;
25 import org.apache.accumulo.core.security.tokens.PasswordToken;
26 import org.apache.accumulo.core.util.shell.Shell;
27 import org.apache.accumulo.core.util.shell.Shell.Command;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31
32 public class PasswdCommand extends Command {
33 private Option userOpt;
34
35 @Override
36 public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, IOException {
37 final String currentUser = shellState.getConnector().whoami();
38 final String user = cl.getOptionValue(userOpt.getOpt(), currentUser);
39
40 String password = null;
41 String passwordConfirm = null;
42 String oldPassword = null;
43
44 oldPassword = shellState.readMaskedLine("Enter current password for '" + currentUser + "': ", '*');
45 if (oldPassword == null) {
46 shellState.getReader().printNewline();
47 return 0;
48 }
49
50 if (!shellState.getConnector().securityOperations().authenticateUser(currentUser, oldPassword.getBytes()))
51 throw new AccumuloSecurityException(user, SecurityErrorCode.BAD_CREDENTIALS);
52
53 password = shellState.readMaskedLine("Enter new password for '" + user + "': ", '*');
54 if (password == null) {
55 shellState.getReader().printNewline();
56 return 0;
57 }
58 passwordConfirm = shellState.readMaskedLine("Please confirm new password for '" + user + "': ", '*');
59 if (passwordConfirm == null) {
60 shellState.getReader().printNewline();
61 return 0;
62 }
63
64 if (!password.equals(passwordConfirm)) {
65 throw new IllegalArgumentException("Passwords do not match");
66 }
67 byte[] pass = password.getBytes();
68 shellState.getConnector().securityOperations().changeUserPassword(user, pass);
69
70
71 if (shellState.getConnector().whoami().equals(user)) {
72 shellState.updateUser(CredentialHelper.create(user, new PasswordToken().setPassword(pass), shellState.getConnector().getInstance().getInstanceID()));
73 }
74 Shell.log.debug("Changed password for user " + user);
75 return 0;
76 }
77
78 @Override
79 public String description() {
80 return "changes a user's password";
81 }
82
83 @Override
84 public Options getOptions() {
85 final Options o = new Options();
86 userOpt = new Option(Shell.userOption, "user", true, "user to operate on");
87 userOpt.setArgName("user");
88 o.addOption(userOpt);
89 return o;
90 }
91
92 @Override
93 public int numArgs() {
94 return 0;
95 }
96 }