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