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