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