View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      } // user canceled
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      } // user canceled
58      passwordConfirm = shellState.readMaskedLine("Please confirm new password for '" + user + "': ", '*');
59      if (passwordConfirm == null) {
60        shellState.getReader().printNewline();
61        return 0;
62      } // user canceled
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      // update the current credentials if the password changed was for
70      // the current user
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  }