001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.deployment.cli;
019
020 import org.apache.geronimo.common.DeploymentException;
021 import org.apache.geronimo.util.SimpleEncryption;
022
023 import java.io.PrintWriter;
024 import java.io.File;
025 import java.io.InputStream;
026 import java.io.FileInputStream;
027 import java.io.BufferedInputStream;
028 import java.io.OutputStream;
029 import java.io.BufferedOutputStream;
030 import java.io.FileOutputStream;
031 import java.io.IOException;
032 import java.util.Properties;
033
034 /**
035 * The CLI deployer logic to start.
036 *
037 * @version $Rev: 405624 $ $Date: 2006-05-09 21:08:49 -0700 (Tue, 09 May 2006) $
038 */
039 public class CommandLogin extends AbstractCommand {
040 public CommandLogin() {
041 super("login", "1. Common Commands", "",
042 "Saves the username and password for this connection to the "+
043 "file .geronimo-deployer in the current user's home directory. " +
044 "Future connections to the same server will try to use this "+
045 "saved authentication information instead of prompting where " +
046 "possible. This information is saved separately per connection " +
047 "URL, so you can specify --url or --host and/or --port on the command " +
048 "line to save a login to a different server.\n" +
049 "WARNING: while the login information is not saved in " +
050 "clear text, it is not secure either. If you want to " +
051 "save the authentication securely, you should change the " +
052 ".geronimo-deployer file in your home directory so that nobody " +
053 "else can read or write it.");
054 }
055
056 public CommandLogin(String command, String group, String helpArgumentList, String helpText) {
057 super(command, group, helpArgumentList, helpText);
058 }
059
060 public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
061 try {
062 File authFile = new File(System.getProperty("user.home"), ".geronimo-deployer");
063 if(!authFile.exists()) {
064 if(!authFile.createNewFile()) {
065 throw new DeploymentException("Unable to create "+authFile.getAbsolutePath()+" to hold saved logins");
066 }
067 }
068 if(!authFile.canRead() || !authFile.canWrite()) {
069 throw new DeploymentException("Saved login file "+authFile.getAbsolutePath()+" is not readable or not writable");
070 }
071 Properties props = new Properties();
072 InputStream in = new BufferedInputStream(new FileInputStream(authFile));
073 props.load(in);
074 in.close();
075 props.setProperty("login."+connection.getServerURI(), "{Standard}"+SimpleEncryption.encrypt(connection.getAuthentication()));
076 OutputStream save = new BufferedOutputStream(new FileOutputStream(authFile));
077 props.store(save, "Saved authentication information to connect to Geronimo servers");
078 save.flush();
079 save.close();
080 System.out.print(DeployUtils.reformat("Saved login for: "+connection.getServerURI(), 4, 72));
081 } catch (IOException e) {
082 throw new DeploymentException("Unable to save authentication to login file", e);
083 }
084 }
085 }