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