//Copyright (C) 2012 The Android Open Source Project // //Licensed under the Apache License, Version 2.0 (the "License"); //you may not use this file except in compliance with the License. //You may obtain a copy of the License at // //http://www.apache.org/licenses/LICENSE-2.0 // //Unless required by applicable law or agreed to in writing, software //distributed under the License is distributed on an "AS IS" BASIS, //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //See the License for the specific language governing permissions and //limitations under the License. package com.gitblit.transport.ssh.commands; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; import com.gitblit.transport.ssh.CommandMetaData; import com.gitblit.transport.ssh.IKeyManager; import com.gitblit.transport.ssh.SshKeyAuthenticator; import com.google.common.base.Charsets; /** Set a user's account settings. **/ @CommandMetaData(name = "set-account", description = "Change an account's settings") public class SetAccountCommand extends SshCommand { private static final String ALL = "ALL"; @Argument(index = 0, required = true, metaVar = "USER", usage = "full name, email-address, ssh username or account id") private String user; @Option(name = "--add-ssh-key", metaVar = "-|KEY", usage = "public keys to add to the account") private List addSshKeys = new ArrayList(); @Option(name = "--delete-ssh-key", metaVar = "-|KEY", usage = "public keys to delete from the account") private List deleteSshKeys = new ArrayList(); @Override public void run() throws IOException, UnloggedFailure { validate(); setAccount(); } private void validate() throws UnloggedFailure { if (addSshKeys.contains("-") && deleteSshKeys.contains("-")) { throw new UnloggedFailure(1, "Only one option may use the stdin"); } if (deleteSshKeys.contains(ALL)) { deleteSshKeys = Collections.singletonList(ALL); } } private void setAccount() throws IOException, UnloggedFailure { addSshKeys = readSshKey(addSshKeys); if (!addSshKeys.isEmpty()) { addSshKeys(addSshKeys); } deleteSshKeys = readSshKey(deleteSshKeys); if (!deleteSshKeys.isEmpty()) { deleteSshKeys(deleteSshKeys); } authenticator.getKeyCache().invalidate(user); } private void addSshKeys(List sshKeys) throws UnloggedFailure, IOException { IKeyManager keyManager = authenticator.getKeyManager(); for (String sshKey : sshKeys) { keyManager.addKey(user, sshKey); } } private void deleteSshKeys(List sshKeys) { IKeyManager keyManager = authenticator.getKeyManager(); if (sshKeys.contains(ALL)) { keyManager.removeAllKey(user); } else { for (String sshKey : sshKeys) { keyManager.removeKey(user, sshKey); } } } private List readSshKey(List sshKeys) throws UnsupportedEncodingException, IOException { if (!sshKeys.isEmpty()) { String sshKey; int idx = sshKeys.indexOf("-"); if (idx >= 0) { sshKey = ""; BufferedReader br = new BufferedReader(new InputStreamReader( in, Charsets.UTF_8)); String line; while ((line = br.readLine()) != null) { sshKey += line + "\n"; } sshKeys.set(idx, sshKey); } } return sshKeys; } private SshKeyAuthenticator authenticator; public void setAuthenticator(SshKeyAuthenticator authenticator) { this.authenticator = authenticator; } }