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    
033    /**
034     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
035     * @version $Revision$
036     */
037    public class SSHPlugin extends CRaSHPlugin<SSHPlugin> implements Service {
038    
039      /** . */
040      private SSHLifeCycle lifeCycle;
041    
042      @Override
043      public SSHPlugin getImplementation() {
044        return this;
045      }
046    
047      @Override
048      public void init() {
049    
050        //
051        Integer port = getContext().getProperty(PropertyDescriptor.SSH_PORT);
052        if (port == null) {
053          log.info("Could not boot SSHD due to missing due to missing port configuration");
054          return;
055        }
056    
057        //
058        Resource res = getContext().loadResource("hostkey.pem", ResourceKind.KEY);
059        URL keyURL = null;
060        if (res != null) {
061          keyURL = res.getURL();
062          log.debug("Found key url " + keyURL);
063        }
064    
065        // If we have a key path, we convert is as an URL
066        String keyPath = getContext().getProperty(PropertyDescriptor.SSH_KEYPATH);
067        if (keyPath != null) {
068          log.debug("Found key path " + keyPath);
069          File f = new File(keyPath);
070          if (f.exists() && f.isFile()) {
071            try {
072              keyURL = f.toURI().toURL();
073            } catch (MalformedURLException e) {
074              log.debug("Ignoring invalid key " + keyPath, e);
075            }
076          } else {
077            log.debug("Ignoring invalid key path " + keyPath);
078          }
079        }
080    
081        //
082        if (keyURL == null) {
083          log.info("Could not boot SSHD due to missing key");
084          return;
085        }
086    
087        //
088        log.info("Booting SSHD");
089        SSHLifeCycle lifeCycle = new SSHLifeCycle(getContext());
090        lifeCycle.setPort(port);
091        lifeCycle.setKeyURL(keyURL);
092        lifeCycle.init();
093    
094        //
095        this.lifeCycle = lifeCycle;
096      }
097    
098      @Override
099      public void destroy() {
100        if (lifeCycle != null) {
101          log.info("Shutting down SSHD");
102          lifeCycle.destroy();
103          lifeCycle = null;
104        }
105      }
106    }