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