001    /*
002     * Copyright (C) 2003-2009 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.ssh.term;
020    
021    import org.apache.sshd.SshServer;
022    import org.apache.sshd.common.Session;
023    import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
024    import org.apache.sshd.server.PasswordAuthenticator;
025    import org.apache.sshd.server.session.ServerSession;
026    import org.crsh.plugin.PluginContext;
027    import org.crsh.ssh.term.scp.SCPCommandFactory;
028    import org.crsh.term.TermLifeCycle;
029    import org.crsh.term.spi.TermIOHandler;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    import java.net.URL;
034    
035    /**
036     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
037     * @version $Revision$
038     */
039    public class SSHLifeCycle extends TermLifeCycle {
040    
041      /** . */
042      public static final Session.AttributeKey<String> USERNAME = new Session.AttributeKey<java.lang.String>();
043    
044      /** . */
045      public static final Session.AttributeKey<String> PASSWORD = new Session.AttributeKey<java.lang.String>();
046    
047      /** . */
048      private final Logger log = LoggerFactory.getLogger(SSHLifeCycle.class);
049    
050      /** . */
051      private SshServer server;
052    
053      /** . */
054      private int port;
055    
056      /** . */
057      private URL keyURL;
058    
059      public SSHLifeCycle(PluginContext context) {
060        super(context);
061      }
062    
063      public int getPort() {
064        return port;
065      }
066    
067      public void setPort(int port) {
068        this.port = port;
069      }
070    
071      public URL getKeyURL() {
072        return keyURL;
073      }
074    
075      public void setKeyURL(URL keyURL) {
076        this.keyURL = keyURL;
077      }
078    
079      @Override
080      protected void doInit() {
081        try {
082    
083          //
084          TermIOHandler handler = getHandler();
085    
086          //
087          SshServer server = SshServer.setUpDefaultServer();
088          server.setPort(port);
089          server.setShellFactory(new CRaSHCommandFactory(handler));
090          server.setCommandFactory(new SCPCommandFactory(getContext()));
091          server.setKeyPairProvider(new URLKeyPairProvider(keyURL));
092    
093          //
094          server.setPasswordAuthenticator(new PasswordAuthenticator() {
095            public boolean authenticate(String _username, String _password, ServerSession session) {
096              session.setAttribute(USERNAME, _username);
097              session.setAttribute(PASSWORD, _password);
098              return true;
099            }
100          });
101    
102          //
103          log.info("About to start CRaSSHD");
104          server.start();
105          log.info("CRaSSHD started on port " + port);
106    
107          //
108          this.server = server;
109        }
110        catch (Throwable e) {
111          log.error("Could not start CRaSSHD", e);
112        }
113      }
114    
115      @Override
116      protected void doDestroy() {
117        if (server != null) {
118          try {
119            server.stop();
120          }
121          catch (InterruptedException e) {
122            log.debug("Got an interruption when stopping server", e);
123          }
124        }
125      }
126    }