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
020package org.crsh.ssh;
021
022import org.crsh.auth.AuthenticationPlugin;
023import org.crsh.plugin.CRaSHPlugin;
024import org.crsh.plugin.PropertyDescriptor;
025import org.crsh.plugin.ResourceKind;
026import org.crsh.ssh.term.SSHLifeCycle;
027import org.crsh.vfs.Resource;
028
029import java.io.File;
030import java.io.IOException;
031import java.net.MalformedURLException;
032import java.net.URL;
033import java.util.Arrays;
034import java.util.logging.Level;
035
036import org.apache.sshd.common.util.SecurityUtils;
037
038public class SSHPlugin extends CRaSHPlugin<SSHPlugin> {
039
040  /** The SSH port. */
041  public static final PropertyDescriptor<Integer> SSH_PORT = PropertyDescriptor.create("ssh.port", 2000, "The SSH port");
042
043  /** The SSH server key path. */
044  public static final PropertyDescriptor<String> SSH_SERVER_KEYPATH = PropertyDescriptor.create("ssh.keypath", (String)null, "The path to the key file");
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_SERVER_KEYPATH, AuthenticationPlugin.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.log(Level.INFO, "Could not boot SSHD due to missing due to missing port configuration");
067      return;
068    }
069
070    //
071    Resource serverKey = null;
072
073    // Get embedded default key
074    URL serverKeyURL = SSHPlugin.class.getResource("/crash/hostkey.pem");
075    if (serverKeyURL != null) {
076      try {
077        log.log(Level.FINE, "Found embedded key url " + serverKeyURL);
078        serverKey = new Resource("hostkey.pem", serverKeyURL);
079      }
080      catch (IOException e) {
081        log.log(Level.FINE, "Could not load ssh key from url " + serverKeyURL, e);
082      }
083    }
084
085    // Override from config if any
086    Resource serverKeyRes = getContext().loadResource("hostkey.pem", ResourceKind.CONFIG);
087    if (serverKeyRes != null) {
088      serverKey = serverKeyRes;
089      log.log(Level.FINE, "Found server ssh key url");
090    }
091
092    // If we have a key path, we convert is as an URL
093    String serverKeyPath = getContext().getProperty(SSH_SERVER_KEYPATH);
094    if (serverKeyPath != null) {
095      log.log(Level.FINE, "Found server key path " + serverKeyPath);
096      File f = new File(serverKeyPath);
097      if (f.exists() && f.isFile()) {
098        try {
099          serverKeyURL = f.toURI().toURL();
100        } catch (MalformedURLException e) {
101          log.log(Level.FINE, "Ignoring invalid server key " + serverKeyPath, e);
102        }
103      } else {
104        log.log(Level.FINE, "Ignoring invalid server key path " + serverKeyPath);
105      }
106    }
107
108    //
109    if (serverKeyURL == null) {
110      log.log(Level.INFO, "Could not boot SSHD due to missing server key");
111      return;
112    }
113
114    // Get the authentication
115    AuthenticationPlugin authPlugin = AuthenticationPlugin.NULL;
116    String authentication = getContext().getProperty(AuthenticationPlugin.AUTH);
117    if (authentication != null) {
118      for (AuthenticationPlugin authenticationPlugin : getContext().getPlugins(AuthenticationPlugin.class)) {
119        if (authentication.equals(authenticationPlugin.getName())) {
120          authPlugin = authenticationPlugin;
121          break;
122        }
123      }
124    }
125
126    //
127    log.log(Level.INFO, "Booting SSHD");
128    SSHLifeCycle lifeCycle = new SSHLifeCycle(getContext(), authPlugin);
129    lifeCycle.setPort(port);
130    lifeCycle.setKey(serverKey);
131    lifeCycle.init();
132
133    //
134    this.lifeCycle = lifeCycle;
135  }
136
137  @Override
138  public void destroy() {
139    if (lifeCycle != null) {
140      log.log(Level.INFO, "Shutting down SSHD");
141      lifeCycle.destroy();
142      lifeCycle = null;
143    }
144  }
145}