001    /*
002     * Copyright (C) 2010 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    
020    package org.crsh.ssh;
021    
022    import org.crsh.plugin.CRaSHPlugin;
023    import org.crsh.plugin.PropertyDescriptor;
024    import org.crsh.plugin.ResourceKind;
025    import org.crsh.ssh.term.SSHLifeCycle;
026    import org.crsh.vfs.Resource;
027    
028    import java.io.File;
029    import java.net.MalformedURLException;
030    import java.net.URL;
031    import java.util.Arrays;
032    import org.apache.sshd.common.util.SecurityUtils;
033    
034    /**
035     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
036     * @version $Revision$
037     */
038    public class SSHPlugin extends CRaSHPlugin<SSHPlugin> {
039    
040      /** The SSH port. */
041      public static final PropertyDescriptor<Integer> SSH_PORT = PropertyDescriptor.create("ssh.port", 2000, "The SSH port");
042    
043      /** The SSH key path. */
044      public static final PropertyDescriptor<String> SSH_KEYPATH = PropertyDescriptor.create("ssh.keypath", (String)null, "The path to the key file");
045    
046      /** The authentication plugin to use. */
047      public static final PropertyDescriptor<String> AUTH = PropertyDescriptor.create("auth", (String)null, "The authentication plugin");
048    
049      /** . */
050      private SSHLifeCycle lifeCycle;
051    
052      @Override
053      public SSHPlugin getImplementation() {
054        return this;
055      }
056    
057      @Override
058      protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
059        return Arrays.<PropertyDescriptor<?>>asList(SSH_PORT, SSH_KEYPATH, AUTH);
060      }
061    
062      @Override
063      public void init() {
064    
065        SecurityUtils.setRegisterBouncyCastle(true);
066        //
067        Integer port = getContext().getProperty(SSH_PORT);
068        if (port == null) {
069          log.info("Could not boot SSHD due to missing due to missing port configuration");
070          return;
071        }
072    
073        //
074        Resource res = getContext().loadResource("hostkey.pem", ResourceKind.KEY);
075        URL keyURL = null;
076        if (res != null) {
077          keyURL = res.getURL();
078          log.debug("Found key url " + keyURL);
079        }
080    
081        // If we have a key path, we convert is as an URL
082        String keyPath = getContext().getProperty(SSH_KEYPATH);
083        if (keyPath != null) {
084          log.debug("Found key path " + keyPath);
085          File f = new File(keyPath);
086          if (f.exists() && f.isFile()) {
087            try {
088              keyURL = f.toURI().toURL();
089            } catch (MalformedURLException e) {
090              log.debug("Ignoring invalid key " + keyPath, e);
091            }
092          } else {
093            log.debug("Ignoring invalid key path " + keyPath);
094          }
095        }
096    
097        //
098        if (keyURL == null) {
099          log.info("Could not boot SSHD due to missing key");
100          return;
101        }
102    
103        // Get the authentication
104        String authentication = getContext().getProperty(AUTH);
105    
106        //
107        log.info("Booting SSHD");
108        SSHLifeCycle lifeCycle = new SSHLifeCycle(getContext());
109        lifeCycle.setPort(port);
110        lifeCycle.setKeyURL(keyURL);
111        lifeCycle.setAuthentication(authentication);
112        lifeCycle.init();
113    
114        //
115        this.lifeCycle = lifeCycle;
116      }
117    
118      @Override
119      public void destroy() {
120        if (lifeCycle != null) {
121          log.info("Shutting down SSHD");
122          lifeCycle.destroy();
123          lifeCycle = null;
124        }
125      }
126    }