Compare commits

...

10 Commits

Author SHA1 Message Date
mohammed jasem alaajel 21364a3f63
refactor some of the packages, update licenesing 2023-03-17 21:47:17 +04:00
mohammed jasem alaajel a0cafa3e62
Limework fork is born! 2023-03-17 20:43:59 +04:00
Justin Crawford d348e26b49
Fixed a bug where sometimes sessions are disconnected due to some hosts not resolving localhost properly 2020-04-06 06:06:47 -07:00
Justin Crawford e0080fb1a0
Added hover text and click to copy for /mkpasswd
- Added hover text and click to copy so you can now copy your hash
to the clipboard.
- Added default values for the configuration in case none were set for some reason.
- Validate permissions from the config (something I forgot to do)
- Tell the user permission denied if they don't have permission to run the command.
- Added a default pseudo-user for global permissions and rules.
- Updated some verbage in the config comments
2020-03-26 21:50:39 -07:00
Zachery 40f63bdeeb
Added Discord to readme 2020-02-06 12:03:21 -06:00
Justin Crawford 5a247576c4
Formatting 2019-11-23 21:53:58 -08:00
Justin Crawford ae8d5bff1d
Implemented access control for SFTP 2019-11-23 01:49:27 -08:00
Zachery 3ad2a810f8
Update MkpasswdCommand.java 2019-10-16 00:47:53 -05:00
Zachery de9d723416
Update README.md 2019-10-16 00:42:09 -05:00
Justin Crawford 1c93b9fc0e
Increment project version for sponge release 2019-10-13 01:03:48 -07:00
31 changed files with 900 additions and 527 deletions

View File

@ -1,68 +0,0 @@
---
#BasedOnStyle: WebKit
TabWidth: '4'
IndentWidth: '4'
UseTab: 'Always'
AlignOperands: 'true'
AlignAfterOpenBracket: 'Align'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignEscapedNewlines: 'Left'
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: 'All'
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: 'None'
AlwaysBreakTemplateDeclarations: 'true'
AlwaysBreakBeforeMultilineStrings: 'false'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBraces: 'Custom'
BraceWrapping:
AfterEnum: 'true'
AfterClass: 'true'
AfterControlStatement: 'true'
AfterStruct: 'true'
AfterFunction: 'true'
AfterNamespace: 'true'
AfterUnion: 'true'
AfterExternBlock: 'true'
BeforeCatch: 'true'
BeforeElse: 'true'
SplitEmptyRecord: 'false'
SplitEmptyNamespace: 'false'
SplitEmptyFunction: 'false'
BreakBeforeBinaryOperators: 'true'
BreakBeforeTernaryOperators: 'false'
BreakConstructorInitializersBeforeComma: 'false'
BreakBeforeInheritanceComma: 'false'
BreakStringLiterals: 'true'
ColumnLimit: '140'
CompactNamespaces: 'false'
Cpp11BracedListStyle: 'true'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
DerivePointerAlignment: 'false'
IndentCaseLabels: 'true'
IndentPPDirectives: 'AfterHash'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
Language: 'Java'
NamespaceIndentation: 'All'
PointerAlignment: 'Right'
ReflowComments: 'true'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: 'Auto'
...

View File

@ -1,5 +0,0 @@
sudo: false
dist: trusty
language: java
jdk:
- oraclejdk8

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ryanmichela</groupId>
<artifactId>sshd</artifactId>
<version>2.1.0</version>
</parent>
<artifactId>Minecraft-SSHD-Bukkit</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- License: This module extends bukkit which is GPL v2 -->
<licenses>
<license>
<name>GPL2</name>
<url>http://www.gnu.org/licenses/gpl-2.0.html</url>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- Build -->
<build>
<defaultGoal>clean package</defaultGoal>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>motd.txt</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.name}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<!-- Compile plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -61,7 +61,8 @@ import java.security.SecureRandom;
* @author Damien Miller
* @version 0.2
*/
public class BCrypt {
public class BCrypt
{
// BCrypt parameters
private static final int GENSALT_DEFAULT_LOG2_ROUNDS = 10;
private static final int BCRYPT_SALT_LEN = 16;
@ -383,8 +384,8 @@ public class BCrypt {
* @return base64-encoded string
* @exception IllegalArgumentException if the length is invalid
*/
private static String encode_base64(byte d[], int len)
throws IllegalArgumentException {
private static String encode_base64(byte d[], int len) throws IllegalArgumentException
{
int off = 0;
StringBuffer rs = new StringBuffer();
int c1, c2;
@ -392,19 +393,23 @@ public class BCrypt {
if (len <= 0 || len > d.length)
throw new IllegalArgumentException ("Invalid len");
while (off < len) {
while (off < len)
{
c1 = d[off++] & 0xff;
rs.append(base64_code[(c1 >> 2) & 0x3f]);
c1 = (c1 & 0x03) << 4;
if (off >= len) {
if (off >= len)
{
rs.append(base64_code[c1 & 0x3f]);
break;
}
c2 = d[off++] & 0xff;
c1 |= (c2 >> 4) & 0x0f;
rs.append(base64_code[c1 & 0x3f]);
c1 = (c2 & 0x0f) << 2;
if (off >= len) {
if (off >= len)
{
rs.append(base64_code[c1 & 0x3f]);
break;
}
@ -422,9 +427,11 @@ public class BCrypt {
* @param x the base64-encoded value
* @return the decoded value of x
*/
private static byte char64(char x) {
private static byte char64(char x)
{
if ((int)x < 0 || (int)x > index_64.length)
return -1;
return index_64[(int)x];
}
@ -437,8 +444,8 @@ public class BCrypt {
* @return an array containing the decoded bytes
* @throws IllegalArgumentException if maxolen is invalid
*/
private static byte[] decode_base64(String s, int maxolen)
throws IllegalArgumentException {
private static byte[] decode_base64(String s, int maxolen) throws IllegalArgumentException
{
StringBuffer rs = new StringBuffer();
int off = 0, slen = s.length(), olen = 0;
byte ret[];
@ -447,7 +454,8 @@ public class BCrypt {
if (maxolen <= 0)
throw new IllegalArgumentException ("Invalid maxolen");
while (off < slen - 1 && olen < maxolen) {
while (off < slen - 1 && olen < maxolen)
{
c1 = char64(s.charAt(off++));
c2 = char64(s.charAt(off++));
if (c1 == -1 || c2 == -1)
@ -475,6 +483,7 @@ public class BCrypt {
ret = new byte[olen];
for (off = 0; off < olen; off++)
ret[off] = (byte)rs.charAt(off);
return ret;
}
@ -484,11 +493,13 @@ public class BCrypt {
* @param lr an array containing the two 32-bit half blocks
* @param off the position in the array of the blocks
*/
private final void encipher(int lr[], int off) {
private final void encipher(int lr[], int off)
{
int i, n, l = lr[off], r = lr[off + 1];
l ^= P[0];
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) {
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;)
{
// Feistel substitution on left word
n = S[(l >> 24) & 0xff];
n += S[0x100 | ((l >> 16) & 0xff)];
@ -514,12 +525,14 @@ public class BCrypt {
* current offset into data
* @return the next word of material from data
*/
private static int streamtoword(byte data[], int offp[]) {
private static int streamtoword(byte data[], int offp[])
{
int i;
int word = 0;
int off = offp[0];
for (i = 0; i < 4; i++) {
for (i = 0; i < 4; i++)
{
word = (word << 8) | (data[off] & 0xff);
off = (off + 1) % data.length;
}
@ -531,7 +544,8 @@ public class BCrypt {
/**
* Initialise the Blowfish key schedule
*/
private void init_key() {
private void init_key()
{
P = (int[])P_orig.clone();
S = (int[])S_orig.clone();
}
@ -540,7 +554,8 @@ public class BCrypt {
* Key the Blowfish cipher
* @param key an array containing the key
*/
private void key(byte key[]) {
private void key(byte key[])
{
int i;
int koffp[] = { 0 };
int lr[] = { 0, 0 };
@ -549,13 +564,15 @@ public class BCrypt {
for (i = 0; i < plen; i++)
P[i] = P[i] ^ streamtoword(key, koffp);
for (i = 0; i < plen; i += 2) {
for (i = 0; i < plen; i += 2)
{
encipher(lr, 0);
P[i] = lr[0];
P[i + 1] = lr[1];
}
for (i = 0; i < slen; i += 2) {
for (i = 0; i < slen; i += 2)
{
encipher(lr, 0);
S[i] = lr[0];
S[i + 1] = lr[1];
@ -569,7 +586,8 @@ public class BCrypt {
* @param data salt information
* @param key password information
*/
private void ekskey(byte data[], byte key[]) {
private void ekskey(byte data[], byte key[])
{
int i;
int koffp[] = { 0 }, doffp[] = { 0 };
int lr[] = { 0, 0 };
@ -578,7 +596,8 @@ public class BCrypt {
for (i = 0; i < plen; i++)
P[i] = P[i] ^ streamtoword(key, koffp);
for (i = 0; i < plen; i += 2) {
for (i = 0; i < plen; i += 2)
{
lr[0] ^= streamtoword(data, doffp);
lr[1] ^= streamtoword(data, doffp);
encipher(lr, 0);
@ -586,7 +605,8 @@ public class BCrypt {
P[i + 1] = lr[1];
}
for (i = 0; i < slen; i += 2) {
for (i = 0; i < slen; i += 2)
{
lr[0] ^= streamtoword(data, doffp);
lr[1] ^= streamtoword(data, doffp);
encipher(lr, 0);
@ -604,7 +624,8 @@ public class BCrypt {
* of rounds of hashing to apply
* @return an array containing the binary hashed password
*/
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds) {
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds)
{
int rounds, i, j;
int cdata[] = (int[])bf_crypt_ciphertext.clone();
int clen = cdata.length;
@ -612,24 +633,28 @@ public class BCrypt {
if (log_rounds < 4 || log_rounds > 31)
throw new IllegalArgumentException ("Bad number of rounds");
rounds = 1 << log_rounds;
if (salt.length != BCRYPT_SALT_LEN)
throw new IllegalArgumentException ("Bad salt length");
init_key();
ekskey(salt, password);
for (i = 0; i < rounds; i++) {
for (i = 0; i < rounds; i++)
{
key(password);
key(salt);
}
for (i = 0; i < 64; i++) {
for (i = 0; i < 64; i++)
{
for (j = 0; j < (clen >> 1); j++)
encipher(cdata, j << 1);
}
ret = new byte[clen * 4];
for (i = 0, j = 0; i < clen; i++) {
for (i = 0, j = 0; i < clen; i++)
{
ret[j++] = (byte)((cdata[i] >> 24) & 0xff);
ret[j++] = (byte)((cdata[i] >> 16) & 0xff);
ret[j++] = (byte)((cdata[i] >> 8) & 0xff);
@ -645,7 +670,8 @@ public class BCrypt {
* using BCrypt.gensalt)
* @return the hashed password
*/
public static String hashpw(String password, String salt) {
public static String hashpw(String password, String salt)
{
BCrypt B;
String real_salt;
byte passwordb[], saltb[], hashed[];
@ -655,9 +681,11 @@ public class BCrypt {
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
throw new IllegalArgumentException ("Invalid salt version");
if (salt.charAt(2) == '$')
off = 3;
else {
else
{
minor = salt.charAt(2);
if (minor != 'a' || salt.charAt(3) != '$')
throw new IllegalArgumentException ("Invalid salt revision");
@ -670,9 +698,12 @@ public class BCrypt {
rounds = Integer.parseInt(salt.substring(off, off + 2));
real_salt = salt.substring(off + 3, off + 25);
try {
try
{
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
}
catch (UnsupportedEncodingException uee)
{
throw new AssertionError("UTF-8 is not supported");
}
@ -684,14 +715,17 @@ public class BCrypt {
rs.append("$2");
if (minor >= 'a')
rs.append(minor);
rs.append("$");
if (rounds < 10)
rs.append("0");
rs.append(Integer.toString(rounds));
rs.append("$");
rs.append(encode_base64(saltb, saltb.length));
rs.append(encode_base64(hashed,
bf_crypt_ciphertext.length * 4 - 1));
bf_crypt_ciphertext.length * 4 - 1));
return rs.toString();
}
@ -703,7 +737,8 @@ public class BCrypt {
* @param random an instance of SecureRandom to use
* @return an encoded salt value
*/
public static String gensalt(int log_rounds, SecureRandom random) {
public static String gensalt(int log_rounds, SecureRandom random)
{
StringBuffer rs = new StringBuffer();
byte rnd[] = new byte[BCRYPT_SALT_LEN];
@ -712,6 +747,7 @@ public class BCrypt {
rs.append("$2a$");
if (log_rounds < 10)
rs.append("0");
rs.append(Integer.toString(log_rounds));
rs.append("$");
rs.append(encode_base64(rnd, rnd.length));
@ -725,7 +761,8 @@ public class BCrypt {
* 2**log_rounds.
* @return an encoded salt value
*/
public static String gensalt(int log_rounds) {
public static String gensalt(int log_rounds)
{
return gensalt(log_rounds, new SecureRandom());
}
@ -735,7 +772,8 @@ public class BCrypt {
* rounds to apply
* @return an encoded salt value
*/
public static String gensalt() {
public static String gensalt()
{
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
}
@ -746,7 +784,8 @@ public class BCrypt {
* @param hashed the previously-hashed password
* @return true if the passwords match, false otherwise
*/
public static boolean checkpw(String plaintext, String hashed) {
public static boolean checkpw(String plaintext, String hashed)
{
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
}
}

View File

@ -11,8 +11,8 @@ import java.util.Map;
/**
* Copyright 2013 Ryan Michela
*/
public class ConfigPasswordAuthenticator implements PasswordAuthenticator {
public class ConfigPasswordAuthenticator implements PasswordAuthenticator
{
private Map<String, Integer> FailCounts = new HashMap<String, Integer>();
@Override
@ -20,7 +20,7 @@ public class ConfigPasswordAuthenticator implements PasswordAuthenticator {
{
// Depending on our hash type, we have to try and figure out what we're doing.
String HashType = SshdPlugin.instance.getConfig().getString("PasswordType");
String ConfigHash = SshdPlugin.instance.getConfig().getString("Credentials." + username.trim());
String ConfigHash = SshdPlugin.instance.getConfig().getString("Credentials." + username.trim() + ".password");
if (ConfigHash == null)
SshdPlugin.instance.getLogger().warning("Config has no such user: " + username);
@ -70,7 +70,7 @@ public class ConfigPasswordAuthenticator implements PasswordAuthenticator {
}
SshdPlugin.instance.getLogger().info("Failed login for " + username + " using " + HashType + "-based password authentication.");
Integer tries = SshdPlugin.instance.getConfig().getInt("LoginRetries");
Integer tries = SshdPlugin.instance.getConfig().getInt("LoginRetries", 3);
try
{

View File

@ -14,51 +14,63 @@ import java.io.OutputStream;
/**
* Copyright 2013 Ryan Michela
*/
public class ConsoleCommandFactory implements CommandFactory {
public class ConsoleCommandFactory implements CommandFactory
{
@Override
public Command createCommand(ChannelSession cs, String command) {
public Command createCommand(ChannelSession cs, String command)
{
return new ConsoleCommand(command);
}
public class ConsoleCommand implements Command {
public class ConsoleCommand implements Command
{
private String command;
private InputStream in;
private OutputStream out;
private OutputStream err;
private ExitCallback callback;
public ConsoleCommand(String command) {
public ConsoleCommand(String command)
{
this.command = command;
}
public void setInputStream(InputStream in) {
public void setInputStream(InputStream in)
{
this.in = in;
}
public void setOutputStream(OutputStream out) {
public void setOutputStream(OutputStream out)
{
this.out = out;
}
public void setErrorStream(OutputStream err) {
public void setErrorStream(OutputStream err)
{
this.err = err;
}
public void setExitCallback(ExitCallback callback) {
public void setExitCallback(ExitCallback callback)
{
this.callback = callback;
}
@Override
public void start(ChannelSession cs, Environment environment) throws IOException {
try {
public void start(ChannelSession cs, Environment environment) throws IOException
{
try
{
SshdPlugin.instance.getLogger()
.info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
} catch (Exception e) {
}
catch (Exception e)
{
SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage());
} finally {
}
finally
{
callback.onExit(0);
}
}

View File

@ -140,10 +140,10 @@ class Cryptography
private static byte[] FromHex(String hex) throws NoSuchAlgorithmException
{
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++)
{
bytes[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
}
return bytes;
}
@ -152,6 +152,7 @@ class Cryptography
BigInteger bi = new BigInteger(1, array);
String hex = bi.toString(16);
int paddingLength = (array.length * 2) - hex.length();
if (paddingLength > 0)
return String.format("%0" + paddingLength + "d", 0) + hex;
else

View File

@ -10,39 +10,51 @@ import java.util.logging.*;
/**
* Copyright 2013 Ryan Michela
*/
public class FlushyStreamHandler extends StreamHandler {
public class FlushyStreamHandler extends StreamHandler
{
private ConsoleReader reader;
public FlushyStreamHandler(OutputStream out, Formatter formatter, ConsoleReader reader) {
public FlushyStreamHandler(OutputStream out, Formatter formatter, ConsoleReader reader)
{
super(out, formatter);
this.reader = reader;
setLevel(Level.INFO);
}
@Override
public synchronized void publish(LogRecord record) {
public synchronized void publish(LogRecord record)
{
record.setMessage(record.getMessage().replace("\n", "\n\r"));
super.publish(record);
flush();
}
@Override
public synchronized void flush() {
try {
public synchronized void flush()
{
try
{
reader.print(ConsoleReader.RESET_LINE + "");
reader.flush();
super.flush();
try {
try
{
reader.drawLine();
} catch (Throwable ex) {
}
catch (Throwable ex)
{
reader.getCursorBuffer().clear();
}
reader.flush();
super.flush();
} catch (SshException ex) {
}
catch (SshException ex)
{
// do nothing
} catch (IOException ex) {
}
catch (IOException ex)
{
Logger.getLogger(FlushyStreamHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}

View File

@ -7,6 +7,11 @@ import org.bukkit.entity.Player;
import java.util.Arrays;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import com.ryanmichela.sshd.Cryptography;
import com.ryanmichela.sshd.SshdPlugin;
@ -30,10 +35,6 @@ class MkpasswdCommand implements CommandExecutor
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
// If we're not mkpasswd, just fuck off.
if (!label.equalsIgnoreCase("mkpasswd"))
return false;
String algoritm, password;
try
{
@ -62,7 +63,7 @@ class MkpasswdCommand implements CommandExecutor
if (algoritm.equalsIgnoreCase("PLAIN"))
{
// I mean c'mon...
sender.sendMessage("Bro really? it's literally your unencrypted password...");
sender.sendMessage("\u00A79Your Hash: \u00A7cIt's literally your unhashed password.");
return true;
}
else if (algoritm.equalsIgnoreCase("pbkdf2"))
@ -77,7 +78,11 @@ class MkpasswdCommand implements CommandExecutor
return true;
}
sender.sendMessage("\u00A79Your Hash: " + hash + "\u00A7r");
TextComponent msg = new TextComponent("\u00A79Your Hash: " + hash + "\u00A7r");
msg.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, hash));
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to copy the hash!").create()));
sender.spigot().sendMessage(msg);
}
catch (Exception e)
{
@ -86,7 +91,9 @@ class MkpasswdCommand implements CommandExecutor
sender.sendMessage("\u00A7cAn error occured. Please check console for details.\u00A7r");
}
}
else
sender.sendMessage("\u00A7cPermission Denied.\u00A7r");
return true;
}
}
}

View File

@ -0,0 +1,23 @@
package com.ryanmichela.sshd;
import java.util.Optional;
import com.ryanmichela.sshd.SshdPlugin;
public class PermissionUtil
{
public static Optional<String> GetCredential(String username, String credential)
{
String Default = SshdPlugin.instance.getConfig().getString("Credentials.$default." + credential);
String cred = SshdPlugin.instance.getConfig().getString("Credentials." + username + "." + credential, Default);
if (cred == null)
return Optional.empty();
else if (cred.isEmpty())
return Optional.empty();
else
return Optional.of(cred);
}
};

View File

@ -1,6 +1,5 @@
package com.ryanmichela.sshd;
import org.apache.commons.lang.ArrayUtils;
import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
import org.apache.sshd.common.config.keys.PublicKeyEntryResolver;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
@ -27,7 +26,7 @@ public class PublicKeyAuthenticator implements PublickeyAuthenticator
{
byte[] keyBytes = key.getEncoded();
File keyFile = new File(authorizedKeysDir, username);
Integer tries = SshdPlugin.instance.getConfig().getInt("LoginRetries");
Integer tries = SshdPlugin.instance.getConfig().getInt("LoginRetries", 3);
if (keyFile.exists())
{
@ -68,7 +67,8 @@ public class PublicKeyAuthenticator implements PublickeyAuthenticator
}
catch (Exception e)
{
SshdPlugin.instance.getLogger().severe("Failed to process public key " + keyFile.getAbsolutePath() + " " + e.getMessage());
e.printStackTrace();
SshdPlugin.instance.getLogger().severe("Failed to process public key " + keyFile.getAbsolutePath());
}
}
else

View File

@ -9,16 +9,20 @@ import java.lang.reflect.Modifier;
*/
public class ReflectionUtil {
public static void setProtectedValue(Object o, String field, Object newValue) {
public static void setProtectedValue(Object o, String field, Object newValue)
{
setProtectedValue(o.getClass(), o, field, newValue);
}
public static void setProtectedValue(Class c, String field, Object newValue) {
public static void setProtectedValue(Class c, String field, Object newValue)
{
setProtectedValue(c, null, field, newValue);
}
public static void setProtectedValue(Class c, Object o, String field, Object newValue) {
try {
public static void setProtectedValue(Class c, Object o, String field, Object newValue)
{
try
{
Field f = c.getDeclaredField(field);
@ -29,18 +33,25 @@ public class ReflectionUtil {
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
f.set(o, newValue);
} catch (NoSuchFieldException | IllegalAccessException ex) {
}
catch (NoSuchFieldException | IllegalAccessException ex)
{
System.out.println("*** " + c.getName() + ":" + ex);
}
}
public static <T> T getProtectedValue(Object obj, String fieldName) {
try {
public static <T> T getProtectedValue(Object obj, String fieldName)
{
try
{
Class c = obj.getClass();
while (c != Object.class) {
while (c != Object.class)
{
Field[] fields = c.getDeclaredFields();
for (Field f : fields) {
if (f.getName() == fieldName) {
for (Field f : fields)
{
if (f.getName() == fieldName)
{
f.setAccessible(true);
return (T) f.get(obj);
}
@ -49,46 +60,58 @@ public class ReflectionUtil {
}
System.out.println("*** " + obj.getClass().getName() + ":No such field");
return null;
} catch (Exception ex) {
}
catch (Exception ex)
{
System.out.println("*** " + obj.getClass().getName() + ":" + ex);
return null;
}
}
public static <T> T getProtectedValue(Class c, String field) {
try {
public static <T> T getProtectedValue(Class c, String field)
{
try
{
Field f = c.getDeclaredField(field);
f.setAccessible(true);
return (T) f.get(c);
} catch (Exception ex) {
}
catch (Exception ex)
{
System.out.println("*** " + c.getName() + ":" + ex);
return null;
}
}
public static Object invokeProtectedMethod(Class c, String method, Object... args) {
public static Object invokeProtectedMethod(Class c, String method, Object... args)
{
return invokeProtectedMethod(c, null, method, args);
}
public static Object invokeProtectedMethod(Object o, String method, Object... args) {
public static Object invokeProtectedMethod(Object o, String method, Object... args)
{
return invokeProtectedMethod(o.getClass(), o, method, args);
}
public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args) {
try {
public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args)
{
try
{
Class[] pTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Integer) {
for (int i = 0; i < args.length; i++)
{
if (args[i] instanceof Integer)
pTypes[i] = int.class;
} else {
else
pTypes[i] = args[i].getClass();
}
}
Method m = c.getDeclaredMethod(method, pTypes);
m.setAccessible(true);
return m.invoke(o, args);
} catch (Exception ex) {
}
catch (Exception ex)
{
System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
return null;
}

View File

@ -0,0 +1,210 @@
package com.ryanmichela.sshd;
import com.ryanmichela.sshd.jline.ConsoleShellFactory;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.session.helpers.AbstractSession;
import org.apache.sshd.contrib.server.subsystem.sftp.SimpleAccessControlSftpEventListener;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.apache.sshd.sftp.server.SftpSubsystemFactory;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;
/**
* Copyright 2013 Ryan Michela
*/
public class SshdPlugin extends JavaPlugin
{
private SshServer sshd;
public static SshdPlugin instance;
public static List<ConfigurationSection> GetSections(ConfigurationSection source)
{
if (source == null)
return null;
List<ConfigurationSection> nodes = new ArrayList<ConfigurationSection>();
for (String key : source.getKeys(false))
{
if (source.isConfigurationSection(key))
nodes.add(source.getConfigurationSection(key));
}
return nodes;
}
@Override public void onLoad()
{
saveDefaultConfig();
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
if (!authorizedKeys.exists())
authorizedKeys.mkdirs();
try
{
File motd = new File(getDataFolder(), "motd.txt");
if (!motd.exists())
{
InputStream link = (getClass().getResourceAsStream("/motd.txt"));
Files.copy(link, motd.getAbsoluteFile().toPath());
}
}
catch (IOException e)
{
e.printStackTrace();
}
// Don't go any lower than INFO or SSHD will cause a stack overflow exception.
// SSHD will log that it wrote bites to the output stream, which writes
// bytes to the output stream - ad nauseaum.
getLogger().setLevel(Level.INFO);
}
@Override public void onEnable()
{
instance = this;
sshd = SshServer.setUpDefaultServer();
sshd.setPort(getConfig().getInt("Port", 1025));
String host = getConfig().getString("ListenAddress", "all");
sshd.setHost(host.equals("all") ? null : host);
File hostKey = new File(getDataFolder(), "hostkey");
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKey.toPath()));
sshd.setShellFactory(new ConsoleShellFactory());
sshd.setPasswordAuthenticator(new ConfigPasswordAuthenticator());
sshd.setPublickeyAuthenticator(new PublicKeyAuthenticator(authorizedKeys));
if (getConfig().getBoolean("EnableSFTP", false))
{
// Handle access control for SFTP.
SftpSubsystemFactory.Builder builder = new SftpSubsystemFactory.Builder();
builder.addSftpEventListener(new SimpleAccessControlSftpEventListener()
{
protected boolean isAccessAllowed(ServerSession session, String remote, Path localpath)
{
try
{
ConfigurationSection UsernameNamespace = getConfig().getConfigurationSection("Credentials." + session.getUsername() + ".sftp");
// They don't have SFTP enabled so deny them.
if (UsernameNamespace == null || !UsernameNamespace.getBoolean("enabled"))
return false;
List<ConfigurationSection> rules = GetSections(UsernameNamespace.getConfigurationSection("rules"));
if (rules != null)
{
for (ConfigurationSection path : rules)
{
// Check if the requesting path matches
if (localpath.toString().matches(path.getName()))
{
// Check if they have read permissions
if (path.getBoolean("readable"))
return true;
getLogger().info(String.format("Denied %s read access to \"%s\" matching rule \"%s\"", session.getUsername(), localpath.toString(), path.getName()));
return false;
}
}
}
return UsernameNamespace.getString("default").equalsIgnoreCase("allow");
}
catch (Exception e)
{
e.printStackTrace();
// Automatically deny.
return false;
}
}
protected boolean isModificationAllowed(ServerSession session, String remote, Path localpath)
{
try
{
boolean defaultbool = getConfig().getBoolean("Credentials.$default.sftp.enabled", false);
ConfigurationSection UsernameNamespace = getConfig().getConfigurationSection("Credentials." + session.getUsername() + ".sftp");
// They don't have SFTP enabled so deny them.
if (UsernameNamespace == null || !UsernameNamespace.getBoolean("enabled", defaultbool))
return false;
// Check a list of files against a path trying to be accessed.
List<ConfigurationSection> rules = GetSections(UsernameNamespace.getConfigurationSection("rules"));
if (rules != null)
{
for (ConfigurationSection path : rules)
{
// Check if the requesting path matches
if (localpath.toString().matches(path.getName()))
{
// Check if they have read permissions
if (path.getBoolean("writeable"))
return true;
getLogger().info(String.format("Denied %s modifications to \"%s\" matching rule \"%s\"", session.getUsername(), localpath.toString(), path.getName()));
return false;
}
}
}
return UsernameNamespace.getString("default", "deny").equalsIgnoreCase("allow");
}
catch (Exception e)
{
e.printStackTrace();
// Automatically deny.
return false;
}
}
});
sshd.setSubsystemFactories(Collections.singletonList(builder.build()));
sshd.setFileSystemFactory(new VirtualFileSystemFactory(FileSystems.getDefault().getPath(getDataFolder().getAbsolutePath()).getParent().getParent()));
}
this.getCommand("mkpasswd").setExecutor(new MkpasswdCommand());
sshd.setCommandFactory(new ConsoleCommandFactory());
try
{
sshd.start();
}
catch (IOException e)
{
getLogger().log(Level.SEVERE, "Failed to start SSH server! ", e);
}
}
@Override public void onDisable()
{
try
{
// Terminate any active sessions
for (AbstractSession as : sshd.getActiveSessions())
as.close(true);
// Pass "true" to stop immediately!
sshd.stop(true);
}
catch (Exception e)
{
// do nothing
e.printStackTrace();
}
}
}

View File

@ -13,80 +13,99 @@ import java.util.logging.StreamHandler;
/**
* Copyright 2014 Ryan Michela
*/
public class StreamHandlerAppender implements Appender {
public class StreamHandlerAppender implements Appender
{
private StreamHandler streamHandler;
private UUID uuid;
public StreamHandlerAppender(StreamHandler streamHandler) {
public StreamHandlerAppender(StreamHandler streamHandler)
{
this.streamHandler = streamHandler;
uuid = UUID.randomUUID();
}
@Override
public void append(LogEvent logEvent) {
public void append(LogEvent logEvent)
{
java.util.logging.Level level;
if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.DEBUG)) {
if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.DEBUG))
level = java.util.logging.Level.FINE;
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.INFO)) {
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.INFO))
level = java.util.logging.Level.INFO;
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.WARN)) {
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.WARN))
level = java.util.logging.Level.WARNING;
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.ERROR)) {
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.ERROR))
level = java.util.logging.Level.SEVERE;
} else {
else
level = java.util.logging.Level.INFO;
}
String message = logEvent.getMessage().getFormattedMessage();
LogRecord logRecord = new LogRecord(level, message);
streamHandler.publish(logRecord);
}
@Override
public String getName() {
public String getName()
{
return "StreamHandlerAppender:" + uuid.toString();
}
@Override
public Layout<? extends Serializable> getLayout() {
public Layout<? extends Serializable> getLayout()
{
return null;
}
@Override
public boolean ignoreExceptions() {
public boolean ignoreExceptions()
{
return false;
}
@Override
public ErrorHandler getHandler() {
public ErrorHandler getHandler()
{
return null;
}
@Override
public void setHandler(ErrorHandler errorHandler) {
public void setHandler(ErrorHandler errorHandler)
{
}
@Override
public void start() {
public State getState() {
// Todo: i am not sure what is this
return null;
}
@Override
public void stop() {
public void initialize() {
// Todo: i am not sure what is this
}
@Override
public boolean isStarted() {
public void start()
{
}
@Override
public void stop()
{
}
@Override
public boolean isStarted()
{
return true;
}
@Override
public boolean isStopped() {
public boolean isStopped()
{
return false;
}
}

View File

@ -5,9 +5,11 @@ import java.util.concurrent.ExecutionException;
/**
* Copyright 2013 Ryan Michela
*/
public abstract class Waitable<T> implements Runnable {
public abstract class Waitable<T> implements Runnable
{
private enum Status {
private enum Status
{
WAITING,
RUNNING,
FINISHED,
@ -17,19 +19,28 @@ public abstract class Waitable<T> implements Runnable {
T value = null;
Status status = Status.WAITING;
public final void run() {
synchronized (this) {
if (status != Status.WAITING) {
public final void run()
{
synchronized (this)
{
if (status != Status.WAITING)
throw new IllegalStateException("Invalid state " + status);
}
status = Status.RUNNING;
}
try {
try
{
value = evaluate();
} catch (Throwable t) {
}
catch (Throwable t)
{
this.t = t;
} finally {
synchronized (this) {
}
finally
{
synchronized (this)
{
status = Status.FINISHED;
this.notifyAll();
}
@ -38,13 +49,14 @@ public abstract class Waitable<T> implements Runnable {
protected abstract T evaluate();
public synchronized T get() throws InterruptedException, ExecutionException {
while (status != Status.FINISHED) {
public synchronized T get() throws InterruptedException, ExecutionException
{
while (status != Status.FINISHED)
this.wait();
}
if (t != null) {
if (t != null)
throw new ExecutionException(t);
}
return value;
}
}

View File

@ -1,8 +1,7 @@
package com.ryanmichela.sshd.implementations;
package com.ryanmichela.sshd.implementations.commandsenders;
import com.ryanmichela.sshd.SshdPlugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
@ -15,12 +14,13 @@ import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import com.ryanmichela.sshd.ConsoleShellFactory;
import com.ryanmichela.sshd.jline.ConsoleShellFactory;
import com.ryanmichela.sshd.ConsoleLogFormatter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
public class SSHDCommandSender implements ConsoleCommandSender, CommandSender
@ -61,92 +61,133 @@ public class SSHDCommandSender implements ConsoleCommandSender, CommandSender
}
}
public void sendMessage(String[] messages) {
@Override
public void sendRawMessage(UUID uuid, String s) {
sendRawMessage(s);
}
public void sendMessage(String[] messages)
{
Arrays.asList(messages).forEach(this::sendMessage);
}
public String getName() {
@Override
public void sendMessage(UUID uuid, String s) {
sendMessage(s);
}
@Override
public void sendMessage(UUID uuid, String... strings) {
sendMessage(strings);
}
public String getName()
{
return "SSHD Console";
}
public boolean isOp() {
public boolean isOp()
{
return true;
}
public void setOp(boolean value) {
public void setOp(boolean value)
{
throw new UnsupportedOperationException("Cannot change operator status of server console");
}
public boolean beginConversation(Conversation conversation) {
public boolean beginConversation(Conversation conversation)
{
return this.conversationTracker.beginConversation(conversation);
}
public void abandonConversation(Conversation conversation) {
public void abandonConversation(Conversation conversation)
{
this.conversationTracker.abandonConversation(conversation, new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
}
public void abandonConversation(Conversation conversation, ConversationAbandonedEvent details) {
public void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
{
this.conversationTracker.abandonConversation(conversation, details);
}
public void acceptConversationInput(String input) {
public void acceptConversationInput(String input)
{
this.conversationTracker.acceptConversationInput(input);
}
public boolean isConversing() {
public boolean isConversing()
{
return this.conversationTracker.isConversing();
}
public boolean isPermissionSet(String name) {
public boolean isPermissionSet(String name)
{
return this.perm.isPermissionSet(name);
}
public boolean isPermissionSet(Permission perm) {
public boolean isPermissionSet(Permission perm)
{
return this.perm.isPermissionSet(perm);
}
public boolean hasPermission(String name) {
public boolean hasPermission(String name)
{
return this.perm.hasPermission(name);
}
public boolean hasPermission(Permission perm) {
public boolean hasPermission(Permission perm)
{
return this.perm.hasPermission(perm);
}
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value)
{
return this.perm.addAttachment(plugin, name, value);
}
public PermissionAttachment addAttachment(Plugin plugin) {
public PermissionAttachment addAttachment(Plugin plugin)
{
return this.perm.addAttachment(plugin);
}
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks)
{
return this.perm.addAttachment(plugin, name, value, ticks);
}
public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
public PermissionAttachment addAttachment(Plugin plugin, int ticks)
{
return this.perm.addAttachment(plugin, ticks);
}
public void removeAttachment(PermissionAttachment attachment) {
public void removeAttachment(PermissionAttachment attachment)
{
this.perm.removeAttachment(attachment);
}
public void recalculatePermissions() {
public void recalculatePermissions()
{
this.perm.recalculatePermissions();
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return this.perm.getEffectivePermissions();
}
public boolean isPlayer() {
public boolean isPlayer()
{
return false;
}
public Server getServer() {
public Server getServer()
{
return Bukkit.getServer();
}
public CommandSender.Spigot spigot()
{
return ((CommandSender)this).spigot();
}
}

View File

@ -1,4 +1,4 @@
package com.ryanmichela.sshd.implementations;
package com.ryanmichela.sshd.implementations.commandsenders;
import org.bukkit.Bukkit;
import org.bukkit.conversations.Conversation;
@ -8,13 +8,17 @@ import org.bukkit.conversations.ManuallyAbandonedConversationCanceller;
import java.util.LinkedList;
import java.util.logging.Level;
public class SSHDConversationTracker {
public class SSHDConversationTracker
{
private LinkedList<Conversation> conversationQueue = new LinkedList<>();
synchronized boolean beginConversation(Conversation conversation) {
if (!this.conversationQueue.contains(conversation)) {
synchronized boolean beginConversation(Conversation conversation)
{
if (!this.conversationQueue.contains(conversation))
{
this.conversationQueue.addLast(conversation);
if (this.conversationQueue.getFirst() == conversation) {
if (this.conversationQueue.getFirst() == conversation)
{
conversation.begin();
conversation.outputNextPrompt();
return true;
@ -24,55 +28,66 @@ public class SSHDConversationTracker {
return true;
}
synchronized void abandonConversation(Conversation conversation, ConversationAbandonedEvent details) {
if (!this.conversationQueue.isEmpty()) {
if (this.conversationQueue.getFirst() == conversation) {
synchronized void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
{
if (!this.conversationQueue.isEmpty())
{
if (this.conversationQueue.getFirst() == conversation)
conversation.abandon(details);
}
if (this.conversationQueue.contains(conversation)) {
if (this.conversationQueue.contains(conversation))
this.conversationQueue.remove(conversation);
}
if (!this.conversationQueue.isEmpty()) {
if (!this.conversationQueue.isEmpty())
this.conversationQueue.getFirst().outputNextPrompt();
}
}
}
public synchronized void abandonAllConversations() {
public synchronized void abandonAllConversations()
{
LinkedList<Conversation> oldQueue = this.conversationQueue;
this.conversationQueue = new LinkedList<>();
for (Conversation conversation : oldQueue) {
try {
for (Conversation conversation : oldQueue)
{
try
{
conversation.abandon(new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
} catch (Throwable var5) {
}
catch (Throwable var5)
{
Bukkit.getLogger().log(Level.SEVERE, "Unexpected exception while abandoning a conversation", var5);
}
}
}
synchronized void acceptConversationInput(String input) {
if (this.isConversing()) {
synchronized void acceptConversationInput(String input)
{
if (this.isConversing())
{
Conversation conversation = this.conversationQueue.getFirst();
try {
try
{
conversation.acceptInput(input);
} catch (Throwable var4) {
}
catch (Throwable var4)
{
conversation.getContext().getPlugin().getLogger().log(Level.WARNING, String.format("Plugin %s generated an exception whilst handling conversation input", conversation.getContext().getPlugin().getDescription().getFullName()), var4);
}
}
}
synchronized boolean isConversing() {
synchronized boolean isConversing()
{
return !this.conversationQueue.isEmpty();
}
public synchronized boolean isConversingModaly() {
public synchronized boolean isConversingModaly()
{
return this.isConversing() && this.conversationQueue.getFirst().isModal();
}
}

View File

@ -1,9 +1,12 @@
package com.ryanmichela.sshd;
package com.ryanmichela.sshd.jline;
/**
* Copyright 2013 Ryan Michela
*/
import com.ryanmichela.sshd.ReflectionUtil;
import com.ryanmichela.sshd.SshdPlugin;
import com.ryanmichela.sshd.Waitable;
import jline.console.completer.Completer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
@ -12,33 +15,41 @@ import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
public class ConsoleCommandCompleter implements Completer {
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
Waitable<List<String>> waitable = new Waitable<List<String>>() {
public class ConsoleCommandCompleter implements Completer
{
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates)
{
Waitable<List<String>> waitable = new Waitable<List<String>>()
{
@Override
protected List<String> evaluate() {
protected List<String> evaluate()
{
CommandMap commandMap = ReflectionUtil.getProtectedValue(Bukkit.getServer(), "commandMap");
return commandMap.tabComplete(Bukkit.getServer().getConsoleSender(), buffer);
}
};
Bukkit.getScheduler().runTask(SshdPlugin.instance, waitable);
try {
try
{
List<String> offers = waitable.get();
if (offers == null) {
if (offers == null)
return cursor;
}
candidates.addAll(offers);
final int lastSpace = buffer.lastIndexOf(' ');
if (lastSpace == -1) {
if (lastSpace == -1)
return cursor - buffer.length();
} else {
else
return cursor - (buffer.length() - lastSpace - 1);
}
} catch (ExecutionException e) {
}
catch (ExecutionException e)
{
SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
} catch (InterruptedException e) {
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
return cursor;

View File

@ -1,18 +1,10 @@
package com.ryanmichela.sshd;
package com.ryanmichela.sshd.jline;
import com.ryanmichela.sshd.ConsoleCommandCompleter;
import com.ryanmichela.sshd.ConsoleLogFormatter;
import com.ryanmichela.sshd.FlushyOutputStream;
import com.ryanmichela.sshd.FlushyStreamHandler;
import com.ryanmichela.sshd.SshTerminal;
import com.ryanmichela.sshd.SshdPlugin;
import com.ryanmichela.sshd.StreamHandlerAppender;
import com.ryanmichela.sshd.implementations.SSHDCommandSender;
import com.ryanmichela.sshd.ConsoleLogFormatter;
import com.ryanmichela.sshd.*;
import com.ryanmichela.sshd.implementations.commandsenders.SSHDCommandSender;
import jline.console.ConsoleReader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.sshd.common.Factory;
import org.apache.sshd.server.shell.ShellFactory;
import org.apache.sshd.server.command.Command;
import org.apache.sshd.server.channel.ChannelSession;
@ -28,17 +20,21 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util.StringTokenizer;
import java.net.UnknownHostException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.StreamHandler;
public class ConsoleShellFactory implements ShellFactory {
public class ConsoleShellFactory implements ShellFactory
{
public Command createShell(ChannelSession cs) {
public Command createShell(ChannelSession cs)
{
return new ConsoleShell();
}
public class ConsoleShell implements Command, Runnable {
public class ConsoleShell implements Command, Runnable
{
private InputStream in;
private OutputStream out;
@ -52,35 +48,43 @@ public class ConsoleShellFactory implements ShellFactory {
public ConsoleReader ConsoleReader;
public SSHDCommandSender SshdCommandSender;
public InputStream getIn() {
public InputStream getIn()
{
return in;
}
public OutputStream getOut() {
public OutputStream getOut()
{
return out;
}
public OutputStream getErr() {
public OutputStream getErr()
{
return err;
}
public Environment getEnvironment() {
public Environment getEnvironment()
{
return environment;
}
public void setInputStream(InputStream in) {
public void setInputStream(InputStream in)
{
this.in = in;
}
public void setOutputStream(OutputStream out) {
public void setOutputStream(OutputStream out)
{
this.out = out;
}
public void setErrorStream(OutputStream err) {
public void setErrorStream(OutputStream err)
{
this.err = err;
}
public void setExitCallback(ExitCallback callback) {
public void setExitCallback(ExitCallback callback)
{
this.callback = callback;
}
@ -89,6 +93,17 @@ public class ConsoleShellFactory implements ShellFactory {
{
try
{
String username = env.getEnv().get(Environment.ENV_USER);
Optional<String> optcred = PermissionUtil.GetCredential(username, "console");
// They don't have access.
if (optcred.isPresent() && !optcred.get().contains("R"))
{
cs.close(true);
return;
}
else
SshdPlugin.instance.getLogger().warning("There is no $default pseudo-user under credential, allowing unrestricted access...");
this.ConsoleReader = new ConsoleReader(in, new FlushyOutputStream(out), new SshTerminal());
this.ConsoleReader.setExpandEvents(true);
this.ConsoleReader.addCompleter(new ConsoleCommandCompleter());
@ -99,14 +114,15 @@ public class ConsoleShellFactory implements ShellFactory {
((Logger)LogManager.getRootLogger()).addAppender(this.streamHandlerAppender);
this.environment = env;
this.Username = env.getEnv().get(Environment.ENV_USER);
this.Username = username;
this.SshdCommandSender = new SSHDCommandSender();
this.SshdCommandSender.console = this;
thread = new Thread(this, "SSHD ConsoleShell " + this.Username);
thread = new Thread(this, "SSHD ConsoleShell " + username);
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
throw new IOException("Error starting shell", e);
}
}
@ -118,7 +134,7 @@ public class ConsoleShellFactory implements ShellFactory {
{
try
{
if (!SshdPlugin.instance.getConfig().getString("Mode").equals("RPC"))
if (!SshdPlugin.instance.getConfig().getString("Mode", "DEFAULT").equals("RPC"))
printPreamble(this.ConsoleReader);
while (true)
{
@ -142,11 +158,15 @@ public class ConsoleShellFactory implements ShellFactory {
}
// Hide the mkpasswd command input from other users.
Boolean mkpasswd = command.split(" ")[0].equals("mkpasswd");
Optional<String> optcred = PermissionUtil.GetCredential(this.Username, "console");
if (optcred.isPresent() && !optcred.get().contains("W"))
continue;
Bukkit.getScheduler().runTask(
SshdPlugin.instance, () ->
{
if (SshdPlugin.instance.getConfig().getString("Mode").equals("RPC") && command.startsWith("rpc"))
if (SshdPlugin.instance.getConfig().getString("Mode", "DEFAULT").equals("RPC") && command.startsWith("rpc"))
{
// NO ECHO NO PREAMBLE AND SHIT
String cmd = command.substring("rpc".length() + 1, command.length());
@ -172,6 +192,7 @@ public class ConsoleShellFactory implements ShellFactory {
}
catch (IOException e)
{
e.printStackTrace();
SshdPlugin.instance.getLogger().log(Level.SEVERE, "Error processing command from SSH", e);
}
finally
@ -181,6 +202,20 @@ public class ConsoleShellFactory implements ShellFactory {
}
}
private String GetHostname()
{
try
{
return InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e)
{
e.printStackTrace();
SshdPlugin.instance.getLogger().log(Level.INFO, "The above stacktrace can be ignored, you likely have a misconfigured system hosts file.");
return "Unknown";
}
}
private void printPreamble(ConsoleReader cr) throws IOException
{
File f = new File(SshdPlugin.instance.getDataFolder(), "motd.txt");
@ -194,13 +229,14 @@ public class ConsoleShellFactory implements ShellFactory {
}
catch (FileNotFoundException e)
{
e.printStackTrace();
SshdPlugin.instance.getLogger().log(Level.WARNING, "Could not open " + f + ": File does not exist.");
// Not showing the SSH motd is not a fatal failure, let the session continue.
}
// Doesn't really guarantee our actual system hostname but
// it's better than not having one at all.
cr.println("Connected to: " + InetAddress.getLocalHost().getHostName() + " (" + Bukkit.getServer().getName() + ")\r");
cr.println("Connected to: " + this.GetHostname() + " (" + Bukkit.getServer().getName() + ")\r");
cr.println(ConsoleLogFormatter.ColorizeString(Bukkit.getServer().getMotd()).replaceAll("\n", "\r\n"));
cr.println("\r");
cr.println("Type 'exit' to exit the shell." + "\r");

View File

@ -1,18 +1,20 @@
package com.ryanmichela.sshd;
package com.ryanmichela.sshd.jline;
import jline.TerminalSupport;
/**
* Copyright 2013 Ryan Michela
*/
public class SshTerminal extends TerminalSupport {
protected SshTerminal() {
public class SshTerminal extends TerminalSupport
{
public SshTerminal()
{
super(true);
}
@Override
public void init() throws Exception {
public void init() throws Exception
{
setAnsiSupported(true);
setEchoEnabled(true);
}

View File

@ -0,0 +1,87 @@
# The IP addresses(s) the SSH server will listen on. Use a comma separated list for multiple addresses.
# Leave as "all" for all addresses.
ListenAddress: all
# The port the SSH server will listen on. Note that anything *below* 1024 will require you to run
# the whole minecraft server with elevated privileges (NOT RECOMMENDED).
Port: 1025
# Operational mode. Don't touch if you don't know what you're doing. Can be either DEFAULT or RPC
Mode: DEFAULT
# Enable built-in SFTP server or not. You'll be able to connect and upload/download files via SFTP protocol.
# Might be useful for testing purposes as well , i. e. docker containers.
EnableSFTP: true
# Number of times a person can fail to use an SSH key or enter a password
# before it terminates the connection.
LoginRetries: 3
########################################################################################
# By default, only public key authentication is enabled. This is the most secure mode.
# To authorize a user to login with their public key, install their key using the
# OpenSSH authorized_keys file format in the authorized_users directory. Name the key
# file with the user's username and no extension. Note: If you want to let a user have
# many keys, you can append the keys to their file in authorized_users.
########################################################################################
# For less secure username and password based authentication, complete the sections below.
# Type of hashing to use for the passwords below.
# Options are: PLAIN (insecure), bcrypt, pbkdf2, sha256
#
# You can use the console/in-game command `/mkpasswd [hash] PASSWORD` to
# generate a password hash string then copy it for your passwords below.
# You can also use `/mkpasswd help` to see what algorithms are supported.
PasswordType: bcrypt
# Associate each username with a password hash (or the password if the PasswordType is set to PLAIN)
Credentials:
# The defaults for any user who does not have a specific section.
# Specific user permissions override the $default pseudo-user.
$default:
# Whether they can read or write to the console
console: RW
# SFTP access for anyone.
sftp:
# Whether sftp is allowed at all.
enabled: true
# Whether to allow or deny by default
default: allow
# specific rules for directories
rules:
# Deny the SSHD config folder by default as an example.
"*SSHD/*":
readable: false
writeable: false
# Username (should match SSH key if using key-based authentication)
justasic:
# Password hash from /mkpasswd command
password: $2a$10$Oqk83FrypRrMF35EDeoQDuidJOQEWBE0joEQ7MJFi/Oeg26wQ3fm2
# Whether they can read, write, or have read/write permissions to console.
console: RW
# SFTP access for this user.
sftp:
# Whether SFTP is enabled for this user.
enabled: true
# Whether to deny access by default or allow access by default
default: allow
# Rules regarding their SFTP access.
# These rules are relative to the server root.
# This acts as a chroot for the server root.
# Each path can be an absolute path or a regular expression.
rules:
"/path/to/file":
# Whether the user can read the file over SFTP
readable: true
# Whether the user can write/modify the file over SFTP
writeable: true
"/path/to/regex/*":
readable: true
writeable: false
"/path/to/directory/":
readable: false
writeable: true
"/another/example/path":
readable: false
writeable: false

View File

@ -1,15 +1,13 @@
Minecraft-SSHD
===========
[![Build Status](https://travis-ci.org/Justasic/Minecraft-SSHD.svg?branch=master)](https://travis-ci.org/Justasic/Minecraft-SSHD)
[![Release](https://img.shields.io/github/release/Justasic/Minecraft-SSHD.svg?label=Release&maxAge=60)](https://github.com/Justasic/Minecraft-SSHD/releases/latest)
[![GitHub license](https://img.shields.io/github/license/Justasic/Minecraft-SSHD)](https://github.com/Justasic/Minecraft-SSHD/blob/master/LICENSE)
<img align="left" width="140" height="140" src="docs/ssh_logo.png?raw=true" hspace="5" vspace="5" alt="diskover"><br/>
**Have you ever wished you could remotely access your server's admin console without having to setup a complex remote access system? Now you can with Minecraft-SSHD!**
Minecraft-SSHD securely exposes your BungeeCord admin console and the server filesystem using the SSH protocol - the same protocol that serves as the secure foundation for nearly all remote server administration.<br/>
Minecraft-SSHD securely exposes your Minecraft admin console and the server filesystem using the SSH protocol - the same protocol that serves as the secure foundation for nearly all remote server administration.<br/>
- Compatible with all ssh clients, regardless of operating system.
- Remotely view your server log in real-time.
@ -98,3 +96,6 @@ Minecraft-SSHD uses cryptographic certificates or a secure username and password
## Source Code
[Get the source on GitHub](https://github.com/Justasic/Minecraft-SSHD "Source Code")
## Our Discord
https://discord.gg/DtrJFn

100
pom.xml
View File

@ -7,8 +7,11 @@
<groupId>com.ryanmichela</groupId>
<artifactId>sshd</artifactId>
<description>Minecraft-SSHD: The SSH daemon for Minecraft servers.</description>
<version>1.3.7</version>
<url>https://github.com/Justasic/Minecraft-SSHD/</url>
<modules>
<module>Minecraft-SSHD-Bukkit</module>
</modules>
<version>2.1.0</version>
<url>https://git.limework.net/Limework/Minecraft-SSHD/</url>
<properties>
<java.version>1.8</java.version>
@ -26,37 +29,36 @@
<!-- License -->
<licenses>
<license>
<name>GPL2</name>
<url>http://www.gnu.org/licenses/gpl-2.0.html</url>
<name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.3.0</version>
<version>2.9.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-mina</artifactId>
<version>2.3.0</version>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-contrib</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.3.0</version>
<version>2.9.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
@ -64,7 +66,7 @@
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
<version>2.3.0</version>
<version>2.9.0</version>
</dependency>
<dependency>
@ -76,100 +78,48 @@
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.1.3</version>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.28</version>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>2.12.1</version>
<version>2.14.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0</version>
<version>2.20.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
<version>2.20.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<version>1.15</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Build -->
<build>
<defaultGoal>clean package</defaultGoal>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>motd.txt</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.name}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<!-- Compile plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<packaging>pom</packaging>
</project>

View File

@ -1,105 +0,0 @@
package com.ryanmichela.sshd;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.bukkit.plugin.java.JavaPlugin;
import com.ryanmichela.sshd.ConsoleShellFactory;
import com.ryanmichela.sshd.MkpasswdCommand;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Collections;
import java.util.logging.Level;
/**
* Copyright 2013 Ryan Michela
*/
public
class SshdPlugin extends JavaPlugin
{
private SshServer sshd;
public static SshdPlugin instance;
@Override public void onLoad()
{
saveDefaultConfig();
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
if (!authorizedKeys.exists())
authorizedKeys.mkdirs();
try
{
File motd = new File(getDataFolder(), "motd.txt");
if (!motd.exists())
{
InputStream link = (getClass().getResourceAsStream("/motd.txt"));
Files.copy(link, motd.getAbsoluteFile().toPath());
}
}
catch (IOException e)
{
e.printStackTrace();
}
// Don't go any lower than INFO or SSHD will cause a stack overflow exception.
// SSHD will log that it wrote bites to the output stream, which writes
// bytes to the output stream - ad nauseaum.
getLogger().setLevel(Level.INFO);
}
@Override public void onEnable()
{
instance = this;
sshd = SshServer.setUpDefaultServer();
sshd.setPort(getConfig().getInt("Port", 1025));
String host = getConfig().getString("ListenAddress", "all");
sshd.setHost(host.equals("all") ? null : host);
File hostKey = new File(getDataFolder(), "hostkey");
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKey.toPath()));
sshd.setShellFactory(new ConsoleShellFactory());
sshd.setPasswordAuthenticator(new ConfigPasswordAuthenticator());
sshd.setPublickeyAuthenticator(new PublicKeyAuthenticator(authorizedKeys));
if (getConfig().getBoolean("EnableSFTP"))
{
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
sshd.setFileSystemFactory(
new VirtualFileSystemFactory(FileSystems.getDefault().getPath(getDataFolder().getAbsolutePath()).getParent().getParent()));
}
this.getCommand("mkpasswd").setExecutor(new MkpasswdCommand());
sshd.setCommandFactory(new ConsoleCommandFactory());
try
{
sshd.start();
}
catch (IOException e)
{
getLogger().log(Level.SEVERE, "Failed to start SSH server! ", e);
}
}
@Override public void onDisable()
{
try
{
sshd.stop();
}
catch (Exception e)
{
// do nothing
}
}
}

View File

@ -1,41 +0,0 @@
# The IP addresses(s) the SSH server will listen on. Use a comma separated list for multiple addresses.
# Leave as "all" for all addresses.
ListenAddress: all
# The port the SSH server will listen on. Note that anything above 1024 will require you to run
# the whole minecraft server with elevated privileges, this is not recommended and you should
# use iptables to route packets from a lower port.
Port: 1025
# Operational mode. Don't touch if you don't know what you're doing. Can be either DEFAULT or RPC
Mode: DEFAULT
# Enable built-in SFTP server or not. You'll be able to connect and upload/download files via SFTP protocol.
# Might be useful for testing purposes as well , i. e. docker containers.
EnableSFTP: true
# Number of times a person can fail to use an SSH key or enter a password
# before it terminates the connection.
LoginRetries: 3
########################################################################################
# By default, only public key authentication is enabled. This is the most secure mode.
# To authorize a user to login with their public key, install their key using the
# OpenSSH authorized_keys file format in the authorized_users directory. Name the key
# file with the user's username and no extension. Note: If you want to let a user have
# many keys, you can append the keys to their file in authorized_users.
########################################################################################
# For less secure username and password based authentication, complete the sections below.
# Type of hashing to use for the passwords below.
# Options are: PLAIN (insecure), bcrypt, pbkdf2, sha256
#
# You can use the console/in-game command `/mkpasswd [hash] PASSWORD` to
# generate a password hash string then copy it for your passwords below.
# You can also use `/mkpasswd help` to see what algorithms are supported.
PasswordType: bcrypt
# Associate each username with a password hash (or the password if the PasswordType is set to PLAIN)
Credentials:
# user1: password1
# user2: password2