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