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.server.PasswordAuthenticator;
024    import org.apache.sshd.server.session.ServerSession;
025    import org.crsh.plugin.PluginContext;
026    import org.crsh.auth.AuthenticationPlugin;
027    import org.crsh.ssh.term.scp.SCPCommandFactory;
028    import org.crsh.term.TermLifeCycle;
029    import org.crsh.term.spi.TermIOHandler;
030    import org.crsh.vfs.Resource;
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    /**
035     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
036     * @version $Revision$
037     */
038    public class SSHLifeCycle extends TermLifeCycle {
039    
040      /** . */
041      public static final Session.AttributeKey<String> USERNAME = new Session.AttributeKey<java.lang.String>();
042    
043      /** . */
044      public static final Session.AttributeKey<String> PASSWORD = new Session.AttributeKey<java.lang.String>();
045    
046      /** . */
047      private final Logger log = LoggerFactory.getLogger(SSHLifeCycle.class);
048    
049      /** . */
050      private SshServer server;
051    
052      /** . */
053      private int port;
054    
055      /** . */
056      private Resource key;
057    
058      /** . */
059      private String authentication;
060    
061      public SSHLifeCycle(PluginContext context) {
062        super(context);
063      }
064    
065      public int getPort() {
066        return port;
067      }
068    
069      public void setPort(int port) {
070        this.port = port;
071      }
072    
073      public Resource getKey() {
074        return key;
075      }
076    
077      public void setKey(Resource key) {
078        this.key = key;
079      }
080    
081      public String getAuthentication() {
082        return authentication;
083      }
084    
085      public void setAuthentication(String authentication) {
086        this.authentication = authentication;
087      }
088    
089      @Override
090      protected void doInit() {
091        try {
092    
093          //
094          TermIOHandler handler = getHandler();
095    
096          //
097          SshServer server = SshServer.setUpDefaultServer();
098          server.setPort(port);
099          server.setShellFactory(new CRaSHCommandFactory(handler));
100          server.setCommandFactory(new SCPCommandFactory(getContext()));
101          server.setKeyPairProvider(new URLKeyPairProvider(key));
102    
103          // We never authenticate by default
104          AuthenticationPlugin plugin = new AuthenticationPlugin() {
105            public String getName() {
106              return "null";
107            }
108            public boolean authenticate(String username, String password) throws Exception {
109              return false;
110            }
111          };
112    
113          // Lookup for an authentication plugin
114          if (authentication != null) {
115            for (AuthenticationPlugin authenticationPlugin : getContext().getPlugins(AuthenticationPlugin.class)) {
116              if (authentication.equals(authenticationPlugin.getName())) {
117                plugin = authenticationPlugin;
118                break;
119              }
120            }
121          }
122    
123          //
124          final AuthenticationPlugin authPlugin = plugin;
125    
126          //
127          server.setPasswordAuthenticator(new PasswordAuthenticator() {
128            public boolean authenticate(String _username, String _password, ServerSession session) {
129              boolean auth;
130              if (authPlugin != null)
131              {
132                try {
133                  log.debug("Using authentication plugin " + authPlugin + " to authenticate user " + _username);
134                  auth = authPlugin.authenticate(_username, _password);
135                } catch (Exception e) {
136                  log.error("Exception authenticating user " + _username + " in authentication plugin: " + authPlugin, e);
137                  return false;
138                }
139              }
140              else 
141              {
142                // Say ok as this will be used later for performing an other kind of authentication
143                auth = true;
144              }
145    
146              // We store username and password in session for later reuse
147              session.setAttribute(USERNAME, _username);
148              session.setAttribute(PASSWORD, _password);
149    
150              //
151              return auth;
152            }
153          });
154    
155          //
156          log.info("About to start CRaSSHD");
157          server.start();
158          log.info("CRaSSHD started on port " + port);
159    
160          //
161          this.server = server;
162        }
163        catch (Throwable e) {
164          log.error("Could not start CRaSSHD", e);
165        }
166      }
167    
168      @Override
169      protected void doDestroy() {
170        if (server != null) {
171          try {
172            server.stop();
173          }
174          catch (InterruptedException e) {
175            log.debug("Got an interruption when stopping server", e);
176          }
177        }
178      }
179    }