Compare commits

..

11 Commits

Author SHA1 Message Date
Zachery
766fb45c46
Update MkpasswdCommand.java 2019-10-16 00:48:24 -05:00
Justin Crawford
08528c5127
Increment project version for sponge release 2019-10-13 01:03:22 -07:00
Justin Crawford
8f9cbd55bc
Update for new version release 2019-10-10 19:55:31 -07:00
Zachery
33f34ad54d
Get this to a release state
- Fixed CommandCompleter so tab completion works again.
- Fixed most of the mkpasswd command, it's also now more secure as it
only shows in your SSH session and not other's consoles.
- Removed some now unused code.
2019-10-10 01:12:38 -05:00
Justin Crawford
b51a03e2d5
Add new logo 2019-10-09 23:18:06 -05:00
Zachery
d38c75dd9e SSH on bungee now works.
Had to rewrite the mkpasswd command and how the console was handled
in the SSH session as many areas didn't use CRLF compared to Spigot
which seems to work better. The config had to be done manually for
the plugin since BungeeCord doesn't handle plugin configs as conveniently.

Removed the test server I accidentally added to the git repo.
2019-10-09 22:57:57 -05:00
Zachery
1c6028199b Most of the way there. 2019-10-09 00:20:17 -05:00
Zachery
047369105e
Delete SshdPlugin.java 2019-10-08 16:01:23 -05:00
Zachery
586910a75c PHEW! A lot of the conversion to bungeecord is complete, there's only one more class to complete, but I suspect there will be quite a few bugs. 2019-10-08 15:36:48 -05:00
Justin Crawford
7618333bf3
Update the readme for BungeeCord 2019-10-07 23:42:56 -07:00
Justin Crawford
96bb1a5fa8
Begin the port to BungeeCord 2019-10-07 23:31:20 -07:00
34 changed files with 861 additions and 1481 deletions

54
.clang-format Normal file
View File

@ -0,0 +1,54 @@
---
Language: 'Java'
TabWidth: '4'
IndentWidth: '4'
UseTab: 'Always'
AlignOperands: 'true'
AlignAfterOpenBracket: 'Align'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignConsecutiveMacros: '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: 'Allman'
BreakBeforeBinaryOperators: 'true'
BreakBeforeTernaryOperators: 'false'
BreakConstructorInitializers: 'AfterColon'
BreakBeforeInheritanceComma: 'false'
BreakAfterJavaFieldAnnotations: 'true'
BreakStringLiterals: 'true'
ColumnLimit: '140'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
DerivePointerAlignment: 'false'
IndentCaseLabels: 'true'
IndentPPDirectives: 'AfterHash'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
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'
...

5
.travis.yml Normal file
View File

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

View File

@ -1,91 +0,0 @@
<?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

@ -1,99 +0,0 @@
package com.ryanmichela.sshd;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
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;
class MkpasswdCommand implements CommandExecutor
{
// Because Spigot's failed syntax API is really less than ideal (you should be required to add a
// SendSyntax function override), we're just always going to return true even for syntax failures
// as we will handle the syntax message internally. This also lets us send the messages more
// securely to the client without people knowing we're using the command. This prevents password
// or hash leakages from the user to other connected users. Plus this syntax will show how
// to both use the command and what hashes we support which is important for people who don't
// know how to RTFM. - Justin
private void SendSyntax(CommandSender sender, boolean invalid)
{
if (invalid)
sender.sendMessage("\u00A7cInvalid Syntax\u00A7r");
sender.sendMessage("\u00A7a/mkpasswd <help|hash> <password>\u00A7r");
sender.sendMessage("\u00A79Supported Hashes: SHA256, PBKDF2, BCRYPT, PLAIN\u00A7r");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
String algoritm, password;
try
{
// Stupid bukkit, we have to concatenate the arguments together if they're using
// spaces in their passwords otherwise it won't be as strong as it should be.
algoritm = args[0];
password = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
if (password.trim().isEmpty()) // Shortcut to the catch statement below.
throw new ArrayIndexOutOfBoundsException();
}
catch (ArrayIndexOutOfBoundsException e)
{
// ignore it.
this.SendSyntax(sender, true);
return true;
}
boolean hasperm = (sender instanceof Player) ? ((Player)sender).hasPermission("sshd.mkpasswd") : true;
if (hasperm)
{
try
{
String hash = "";
// Dumb but whatever. Some people are really dense.
if (algoritm.equalsIgnoreCase("PLAIN"))
{
// I mean c'mon...
sender.sendMessage("\u00A79Your Hash: \u00A7cIt's literally your unhashed password.");
return true;
}
else if (algoritm.equalsIgnoreCase("pbkdf2"))
hash = Cryptography.PBKDF2_HashPassword(password);
else if (algoritm.equalsIgnoreCase("bcrypt"))
hash = Cryptography.BCrypt_HashPassword(password);
else if (algoritm.equalsIgnoreCase("sha256"))
hash = Cryptography.SHA256_HashPassword(password);
else
{
this.SendSyntax(sender, !algoritm.equalsIgnoreCase("help"));
return true;
}
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)
{
// We're console, just print the stack trace.
e.printStackTrace();
sender.sendMessage("\u00A7cAn error occured. Please check console for details.\u00A7r");
}
}
else
sender.sendMessage("\u00A7cPermission Denied.\u00A7r");
return true;
}
}

View File

@ -1,23 +0,0 @@
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,119 +0,0 @@
package com.ryanmichela.sshd;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* Copyright 2013 Ryan Michela
*/
public class ReflectionUtil {
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)
{
setProtectedValue(c, null, field, newValue);
}
public static void setProtectedValue(Class c, Object o, String field, Object newValue)
{
try
{
Field f = c.getDeclaredField(field);
f.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
f.set(o, newValue);
}
catch (NoSuchFieldException | IllegalAccessException ex)
{
System.out.println("*** " + c.getName() + ":" + ex);
}
}
public static <T> T getProtectedValue(Object obj, String fieldName)
{
try
{
Class c = obj.getClass();
while (c != Object.class)
{
Field[] fields = c.getDeclaredFields();
for (Field f : fields)
{
if (f.getName() == fieldName)
{
f.setAccessible(true);
return (T) f.get(obj);
}
}
c = c.getSuperclass();
}
System.out.println("*** " + obj.getClass().getName() + ":No such field");
return null;
}
catch (Exception ex)
{
System.out.println("*** " + obj.getClass().getName() + ":" + ex);
return null;
}
}
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)
{
System.out.println("*** " + c.getName() + ":" + ex);
return null;
}
}
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)
{
return invokeProtectedMethod(o.getClass(), o, method, args);
}
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)
pTypes[i] = int.class;
else
pTypes[i] = args[i].getClass();
}
Method m = c.getDeclaredMethod(method, pTypes);
m.setAccessible(true);
return m.invoke(o, args);
}
catch (Exception ex)
{
System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
return null;
}
}
}

View File

@ -1,210 +0,0 @@
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

@ -1,111 +0,0 @@
package com.ryanmichela.sshd;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.ErrorHandler;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import java.io.Serializable;
import java.util.UUID;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;
/**
* Copyright 2014 Ryan Michela
*/
public class StreamHandlerAppender implements Appender
{
private StreamHandler streamHandler;
private UUID uuid;
public StreamHandlerAppender(StreamHandler streamHandler)
{
this.streamHandler = streamHandler;
uuid = UUID.randomUUID();
}
@Override
public void append(LogEvent logEvent)
{
java.util.logging.Level level;
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))
level = java.util.logging.Level.INFO;
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))
level = java.util.logging.Level.SEVERE;
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()
{
return "StreamHandlerAppender:" + uuid.toString();
}
@Override
public Layout<? extends Serializable> getLayout()
{
return null;
}
@Override
public boolean ignoreExceptions()
{
return false;
}
@Override
public ErrorHandler getHandler()
{
return null;
}
@Override
public void setHandler(ErrorHandler errorHandler)
{
}
@Override
public State getState() {
// Todo: i am not sure what is this
return null;
}
@Override
public void initialize() {
// Todo: i am not sure what is this
}
@Override
public void start()
{
}
@Override
public void stop()
{
}
@Override
public boolean isStarted()
{
return true;
}
@Override
public boolean isStopped()
{
return false;
}
}

View File

@ -1,193 +0,0 @@
package com.ryanmichela.sshd.implementations.commandsenders;
import com.ryanmichela.sshd.SshdPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ManuallyAbandonedConversationCanceller;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
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
{
private final PermissibleBase perm = new PermissibleBase(this);
private final SSHDConversationTracker conversationTracker = new SSHDConversationTracker();
// Set by the upstream allocating function
public ConsoleShellFactory.ConsoleShell console;
public void sendMessage(String message)
{
this.sendRawMessage(message + "\r");
}
public void sendRawMessage(String message)
{
// What the fuck does this code even do? Are we sending to one client or all of them?
if (this.console.ConsoleReader == null)
return;
try
{
this.console.ConsoleReader.println(ConsoleLogFormatter.ColorizeString(message).replace("\n", "\n\r"));
this.console.ConsoleReader.print(this.console.ConsoleReader.RESET_LINE + "");
this.console.ConsoleReader.flush();
try
{
this.console.ConsoleReader.drawLine();
}
catch (Throwable ex)
{
this.console.ConsoleReader.getCursorBuffer().clear();
}
this.console.ConsoleReader.flush();
}
catch (IOException e)
{
SshdPlugin.instance.getLogger().log(Level.SEVERE, "Error sending message to SSHDCommandSender", e);
}
}
@Override
public void sendRawMessage(UUID uuid, String s) {
sendRawMessage(s);
}
public void sendMessage(String[] messages)
{
Arrays.asList(messages).forEach(this::sendMessage);
}
@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()
{
return true;
}
public void setOp(boolean value)
{
throw new UnsupportedOperationException("Cannot change operator status of server console");
}
public boolean beginConversation(Conversation conversation)
{
return this.conversationTracker.beginConversation(conversation);
}
public void abandonConversation(Conversation conversation)
{
this.conversationTracker.abandonConversation(conversation, new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
}
public void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
{
this.conversationTracker.abandonConversation(conversation, details);
}
public void acceptConversationInput(String input)
{
this.conversationTracker.acceptConversationInput(input);
}
public boolean isConversing()
{
return this.conversationTracker.isConversing();
}
public boolean isPermissionSet(String name)
{
return this.perm.isPermissionSet(name);
}
public boolean isPermissionSet(Permission perm)
{
return this.perm.isPermissionSet(perm);
}
public boolean hasPermission(String name)
{
return this.perm.hasPermission(name);
}
public boolean hasPermission(Permission perm)
{
return this.perm.hasPermission(perm);
}
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value)
{
return this.perm.addAttachment(plugin, name, value);
}
public PermissionAttachment addAttachment(Plugin plugin)
{
return this.perm.addAttachment(plugin);
}
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)
{
return this.perm.addAttachment(plugin, ticks);
}
public void removeAttachment(PermissionAttachment attachment)
{
this.perm.removeAttachment(attachment);
}
public void recalculatePermissions()
{
this.perm.recalculatePermissions();
}
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return this.perm.getEffectivePermissions();
}
public boolean isPlayer()
{
return false;
}
public Server getServer()
{
return Bukkit.getServer();
}
public CommandSender.Spigot spigot()
{
return ((CommandSender)this).spigot();
}
}

View File

@ -1,93 +0,0 @@
package com.ryanmichela.sshd.implementations.commandsenders;
import org.bukkit.Bukkit;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ManuallyAbandonedConversationCanceller;
import java.util.LinkedList;
import java.util.logging.Level;
public class SSHDConversationTracker
{
private LinkedList<Conversation> conversationQueue = new LinkedList<>();
synchronized boolean beginConversation(Conversation conversation)
{
if (!this.conversationQueue.contains(conversation))
{
this.conversationQueue.addLast(conversation);
if (this.conversationQueue.getFirst() == conversation)
{
conversation.begin();
conversation.outputNextPrompt();
return true;
}
}
return true;
}
synchronized void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
{
if (!this.conversationQueue.isEmpty())
{
if (this.conversationQueue.getFirst() == conversation)
conversation.abandon(details);
if (this.conversationQueue.contains(conversation))
this.conversationQueue.remove(conversation);
if (!this.conversationQueue.isEmpty())
this.conversationQueue.getFirst().outputNextPrompt();
}
}
public synchronized void abandonAllConversations()
{
LinkedList<Conversation> oldQueue = this.conversationQueue;
this.conversationQueue = new LinkedList<>();
for (Conversation conversation : oldQueue)
{
try
{
conversation.abandon(new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
}
catch (Throwable var5)
{
Bukkit.getLogger().log(Level.SEVERE, "Unexpected exception while abandoning a conversation", var5);
}
}
}
synchronized void acceptConversationInput(String input)
{
if (this.isConversing())
{
Conversation conversation = this.conversationQueue.getFirst();
try
{
conversation.acceptInput(input);
}
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()
{
return !this.conversationQueue.isEmpty();
}
public synchronized boolean isConversingModaly()
{
return this.isConversing() && this.conversationQueue.getFirst().isModal();
}
}

View File

@ -1,246 +0,0 @@
package com.ryanmichela.sshd.jline;
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.server.shell.ShellFactory;
import org.apache.sshd.server.command.Command;
import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.bukkit.Bukkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.StreamHandler;
public class ConsoleShellFactory implements ShellFactory
{
public Command createShell(ChannelSession cs)
{
return new ConsoleShell();
}
public class ConsoleShell implements Command, Runnable
{
private InputStream in;
private OutputStream out;
private OutputStream err;
private ExitCallback callback;
private Environment environment;
private Thread thread;
private String Username;
StreamHandlerAppender streamHandlerAppender;
public ConsoleReader ConsoleReader;
public SSHDCommandSender SshdCommandSender;
public InputStream getIn()
{
return in;
}
public OutputStream getOut()
{
return out;
}
public OutputStream getErr()
{
return err;
}
public Environment getEnvironment()
{
return environment;
}
public void setInputStream(InputStream in)
{
this.in = in;
}
public void setOutputStream(OutputStream out)
{
this.out = out;
}
public void setErrorStream(OutputStream err)
{
this.err = err;
}
public void setExitCallback(ExitCallback callback)
{
this.callback = callback;
}
@Override
public void start(ChannelSession cs, Environment env) throws IOException
{
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());
StreamHandler streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), this.ConsoleReader);
this.streamHandlerAppender = new StreamHandlerAppender(streamHandler);
((Logger)LogManager.getRootLogger()).addAppender(this.streamHandlerAppender);
this.environment = env;
this.Username = username;
this.SshdCommandSender = new SSHDCommandSender();
this.SshdCommandSender.console = this;
thread = new Thread(this, "SSHD ConsoleShell " + username);
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
throw new IOException("Error starting shell", e);
}
}
@Override
public void destroy(ChannelSession cs) { ((Logger)LogManager.getRootLogger()).removeAppender(this.streamHandlerAppender); }
public void run()
{
try
{
if (!SshdPlugin.instance.getConfig().getString("Mode", "DEFAULT").equals("RPC"))
printPreamble(this.ConsoleReader);
while (true)
{
String command = this.ConsoleReader.readLine("\r>", null);
// The user sent CTRL+D to close the shell, terminate the session.
if (command == null)
break;
// Skip someone spamming enter
if (command.trim().isEmpty())
continue;
// User wants to exit
if (command.equals("exit") || command.equals("quit"))
break;
// Clear the text from the screen (on supported terminals)
if (command.equals("cls"))
{
this.ConsoleReader.clearScreen();
this.ConsoleReader.drawLine();
this.ConsoleReader.flush();
continue;
}
// 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", "DEFAULT").equals("RPC") && command.startsWith("rpc"))
{
// NO ECHO NO PREAMBLE AND SHIT
String cmd = command.substring("rpc".length() + 1, command.length());
Bukkit.dispatchCommand(this.SshdCommandSender, cmd);
}
else
{
// Don't send our mkpasswd command output. This will echo passwords back
// to the console for all to see. This command is strictly between
// our plugin and the connected client.
if (!mkpasswd)
{
SshdPlugin.instance.getLogger().info("<" + this.Username + "> " + command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
else
{
Bukkit.dispatchCommand(this.SshdCommandSender, command);
}
}
});
}
}
catch (IOException e)
{
e.printStackTrace();
SshdPlugin.instance.getLogger().log(Level.SEVERE, "Error processing command from SSH", e);
}
finally
{
SshdPlugin.instance.getLogger().log(Level.INFO, this.Username + " disconnected from SSH.");
callback.onExit(0);
}
}
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");
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = br.readLine()) != null)
cr.println(ConsoleLogFormatter.ColorizeString(st) + "\r");
}
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: " + 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");
cr.println("===============================================" + "\r");
}
}
}

View File

@ -1,87 +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 *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,13 +1,15 @@
Minecraft-SSHD Minecraft-SSHD (BungeeCord Edition!)
=========== =================================
[![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) [![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/> <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!** **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 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/> 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/>
- Compatible with all ssh clients, regardless of operating system. - Compatible with all ssh clients, regardless of operating system.
- Remotely view your server log in real-time. - Remotely view your server log in real-time.
@ -15,7 +17,7 @@ Minecraft-SSHD securely exposes your Minecraft admin console and the server file
- Supports multiple concurrent remote connections. - Supports multiple concurrent remote connections.
- Strong identity support using public key authentication. - Strong identity support using public key authentication.
- Audit history who is running commands in the console - Audit history who is running commands in the console
- Run Spigot without using screen or tmux (by adding `-noconsole`) - Run BungeeCord without using screen or tmux (by adding `-noconsole`)
- Remotely script your server by issuing one-off console commands with ssh. - Remotely script your server by issuing one-off console commands with ssh.
### Why should I use Minecraft-SSHD? ### Why should I use Minecraft-SSHD?
@ -38,7 +40,7 @@ Screenshots
Setting Up Public Key Authentication Setting Up Public Key Authentication
==================================== ====================================
Setting up public key authentication with SSH requires first generating a public and private key pair and then installing just the public key on your Spigot server. This plugin supports all modern SSH key algoritms as OpenSSH. You can paste as many public keys from the methods below into each user's authorization file if they have multiple private keys. You can read [this guide from ssh.com](https://www.ssh.com/ssh/keygen/) if you want a better explanation on different key files. Setting up public key authentication with SSH requires first generating a public and private key pair and then installing just the public key on your BungeeCord server. This plugin supports all modern SSH key algoritms as OpenSSH. You can paste as many public keys from the methods below into each user's authorization file if they have multiple private keys. You can read [this guide from ssh.com](https://www.ssh.com/ssh/keygen/) if you want a better explanation on different key files.
## Generating New Keys ## Generating New Keys
@ -96,6 +98,3 @@ Minecraft-SSHD uses cryptographic certificates or a secure username and password
## Source Code ## Source Code
[Get the source on GitHub](https://github.com/Justasic/Minecraft-SSHD "Source Code") [Get the source on GitHub](https://github.com/Justasic/Minecraft-SSHD "Source Code")
## Our Discord
https://discord.gg/DtrJFn

130
pom.xml
View File

@ -5,13 +5,9 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.ryanmichela</groupId> <groupId>com.ryanmichela</groupId>
<artifactId>sshd</artifactId> <artifactId>sshd-bungee</artifactId>
<description>Minecraft-SSHD: The SSH daemon for Minecraft servers.</description> <version>2.0.0</version>
<modules> <url>https://github.com/Justasic/Minecraft-SSHD/</url>
<module>Minecraft-SSHD-Bukkit</module>
</modules>
<version>2.1.0</version>
<url>https://git.limework.net/Limework/Minecraft-SSHD/</url>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
@ -29,36 +25,38 @@
<!-- License --> <!-- License -->
<licenses> <licenses>
<license> <license>
<name>Apache-2.0</name> <name>GPL2</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url> <url>http://www.gnu.org/licenses/gpl-2.0.html</url>
</license> </license>
</licenses> </licenses>
<!-- Dependencies --> <!-- Dependencies -->
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.sshd</groupId> <groupId>net.md-5</groupId>
<artifactId>sshd-core</artifactId> <artifactId>bungeecord-api</artifactId>
<version>2.9.0</version> <version>1.14-SNAPSHOT</version>
<scope>compile</scope> <scope>provided</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-mina</artifactId>
<version>2.9.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.sshd</groupId> <groupId>org.apache.sshd</groupId>
<artifactId>sshd-contrib</artifactId> <artifactId>sshd-core</artifactId>
<version>2.9.0</version> <version>2.3.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-mina</artifactId>
<version>2.3.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.sshd</groupId> <groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId> <artifactId>sshd-common</artifactId>
<version>2.9.0</version> <version>2.3.0</version>
<scope>compile</scope> <scope>compile</scope>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
@ -66,7 +64,7 @@
<dependency> <dependency>
<groupId>org.apache.sshd</groupId> <groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId> <artifactId>sshd-sftp</artifactId>
<version>2.9.0</version> <version>2.3.0</version>
</dependency> </dependency>
<dependency> <dependency>
@ -74,52 +72,114 @@
<artifactId>eddsa</artifactId> <artifactId>eddsa</artifactId>
<version>0.3.0</version> <version>0.3.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.mina</groupId> <groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId> <artifactId>mina-core</artifactId>
<version>2.2.1</version> <version>2.1.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>2.0.5</version> <version>1.7.28</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId> <artifactId>slf4j-jdk14</artifactId>
<version>2.0.5</version> <version>1.7.28</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>jline</groupId> <groupId>jline</groupId>
<artifactId>jline</artifactId> <artifactId>jline</artifactId>
<version>2.14.6</version> <version>2.12.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
<version>2.20.0</version> <version>2.0</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId> <artifactId>log4j-api</artifactId>
<version>2.20.0</version> <version>2.1</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-codec</groupId> <groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
<version>1.15</version> <version>1.10</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
</dependencies> </dependencies>
<packaging>pom</packaging> <!-- 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>
</project> </project>

View File

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

View File

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

View File

@ -1,17 +1,15 @@
package com.ryanmichela.sshd.jline; package com.ryanmichela.sshd;
/** /**
* Copyright 2013 Ryan Michela * Copyright 2013 Ryan Michela
*/ */
import net.md_5.bungee.api.plugin.Command;
import com.ryanmichela.sshd.ReflectionUtil;
import com.ryanmichela.sshd.SshdPlugin;
import com.ryanmichela.sshd.Waitable;
import jline.console.completer.Completer; import jline.console.completer.Completer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.logging.Level; import java.util.logging.Level;
@ -19,32 +17,41 @@ public class ConsoleCommandCompleter implements Completer
{ {
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) public int complete(final String buffer, final int cursor, final List<CharSequence> candidates)
{ {
Waitable<List<String>> waitable = new Waitable<List<String>>() Waitable<List<String>> waitable = new Waitable<List<String>>()
{ {
@Override @Override
protected List<String> evaluate() protected List<String> evaluate()
{ {
CommandMap commandMap = ReflectionUtil.getProtectedValue(Bukkit.getServer(), "commandMap"); List<String> tabcomplete = new ArrayList<String>();
return commandMap.tabComplete(Bukkit.getServer().getConsoleSender(), buffer); for (Map.Entry<String, Command> map : SshdPlugin.instance.getProxy().getPluginManager().getCommands())
{
String cmd = map.getKey();
Command value = map.getValue();
if (cmd.startsWith(buffer))
tabcomplete.add(cmd);
}
return tabcomplete;
} }
}; };
Bukkit.getScheduler().runTask(SshdPlugin.instance, waitable); SshdPlugin.instance.getProxy().getScheduler().runAsync(SshdPlugin.instance, waitable);
try try
{ {
List<String> offers = waitable.get(); List<String> offers = waitable.get();
if (offers == null) if (offers == null)
return cursor; return cursor;
candidates.addAll(offers); candidates.addAll(offers);
final int lastSpace = buffer.lastIndexOf(' '); final int lastSpace = buffer.lastIndexOf(' ');
if (lastSpace == -1) if (lastSpace == -1)
return cursor - buffer.length(); return cursor - buffer.length();
else else
return cursor - (buffer.length() - lastSpace - 1); return cursor - (buffer.length() - lastSpace - 1);
} }
catch (ExecutionException e) catch (ExecutionException e)
{ {
SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e); SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
} }

View File

@ -5,7 +5,10 @@ import org.apache.sshd.server.command.CommandFactory;
import org.apache.sshd.server.channel.ChannelSession; import org.apache.sshd.server.channel.ChannelSession;
import org.apache.sshd.server.Environment; import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback; import org.apache.sshd.server.ExitCallback;
import org.bukkit.Bukkit;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -14,45 +17,39 @@ import java.io.OutputStream;
/** /**
* Copyright 2013 Ryan Michela * Copyright 2013 Ryan Michela
*/ */
public class ConsoleCommandFactory implements CommandFactory public class ConsoleCommandFactory implements CommandFactory {
{
@Override @Override
public Command createCommand(ChannelSession cs, String command) public Command createCommand(ChannelSession cs, String command) {
{
return new ConsoleCommand(command); return new ConsoleCommand(command);
} }
public class ConsoleCommand implements Command public class ConsoleCommand implements Command {
{
private String command; private String command;
private InputStream in; private InputStream in;
private OutputStream out; private OutputStream out;
private OutputStream err; private OutputStream err;
private ExitCallback callback; private ExitCallback callback;
public ConsoleCommand(String command) public ConsoleCommand(String command) {
{
this.command = command; this.command = command;
} }
public void setInputStream(InputStream in) public void setInputStream(InputStream in) {
{
this.in = in; this.in = in;
} }
public void setOutputStream(OutputStream out) public void setOutputStream(OutputStream out) {
{
this.out = out; this.out = out;
} }
public void setErrorStream(OutputStream err) public void setErrorStream(OutputStream err) {
{
this.err = err; this.err = err;
} }
public void setExitCallback(ExitCallback callback) public void setExitCallback(ExitCallback callback) {
{
this.callback = callback; this.callback = callback;
} }
@ -63,13 +60,15 @@ public class ConsoleCommandFactory implements CommandFactory
{ {
SshdPlugin.instance.getLogger() SshdPlugin.instance.getLogger()
.info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command); .info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
if (!SshdPlugin.instance.getProxy().getPluginManager().dispatchCommand(SshdPlugin.instance.getProxy().getConsole(), command))
SshdPlugin.instance.getProxy().getConsole().sendMessage(new ComponentBuilder("Command not found").color(ChatColor.RED).create());
} }
catch (Exception e) catch (Exception e)
{ {
SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage()); SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage());
} }
finally finally
{ {
callback.onExit(0); callback.onExit(0);
} }

View File

@ -4,7 +4,7 @@ package com.ryanmichela.sshd;
* Copyright 2013 Ryan Michela * Copyright 2013 Ryan Michela
*/ */
import org.bukkit.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -15,12 +15,13 @@ import java.util.Map;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
public class ConsoleLogFormatter extends Formatter { public class ConsoleLogFormatter extends Formatter
{
private SimpleDateFormat dateFormat; private SimpleDateFormat dateFormat;
private static final Map<ChatColor, String> replacements = new EnumMap<ChatColor, String>(ChatColor.class); private static final Map<ChatColor, String> replacements = new EnumMap<ChatColor, String>(ChatColor.class);
public ConsoleLogFormatter() { public ConsoleLogFormatter()
{
this.dateFormat = new SimpleDateFormat("HH:mm:ss"); this.dateFormat = new SimpleDateFormat("HH:mm:ss");
} }

View File

@ -0,0 +1,208 @@
package com.ryanmichela.sshd;
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.implementations.SSHDCommandSender;
import com.ryanmichela.sshd.ConsoleLogFormatter;
import jline.console.ConsoleReader;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
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;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.StreamHandler;
import static com.ryanmichela.sshd.SshdPlugin.instance;
public class ConsoleShellFactory implements ShellFactory {
public Command createShell(ChannelSession cs) {
return new ConsoleShell();
}
public class ConsoleShell implements Command, Runnable {
private InputStream in;
private OutputStream out;
private OutputStream err;
private ExitCallback callback;
private Environment environment;
private Thread thread;
private String Username;
StreamHandler streamHandler;
public ConsoleReader ConsoleReader;
public SSHDCommandSender SshdCommandSender;
public InputStream getIn() {
return in;
}
public OutputStream getOut() {
return out;
}
public OutputStream getErr() {
return err;
}
public Environment getEnvironment() {
return environment;
}
public void setInputStream(InputStream in) {
this.in = in;
}
public void setOutputStream(OutputStream out) {
this.out = out;
}
public void setErrorStream(OutputStream err) {
this.err = err;
}
public void setExitCallback(ExitCallback callback) {
this.callback = callback;
}
@Override
public void start(ChannelSession cs, Environment env) throws IOException
{
try
{
this.ConsoleReader = new ConsoleReader(in, new FlushyOutputStream(out), new SshTerminal());
this.ConsoleReader.setExpandEvents(true);
this.ConsoleReader.addCompleter(new ConsoleCommandCompleter());
streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), this.ConsoleReader);
SshdPlugin.instance.getProxy().getLogger().addHandler(this.streamHandler);
this.environment = env;
this.Username = env.getEnv().get(Environment.ENV_USER);
this.SshdCommandSender = new SSHDCommandSender();
this.SshdCommandSender.console = this;
thread = new Thread(this, "SSHD ConsoleShell " + this.Username);
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
throw new IOException("Error starting shell", e);
}
}
@Override
public void destroy(ChannelSession cs) { SshdPlugin.instance.getProxy().getLogger().removeHandler(this.streamHandler); }
public void run()
{
try
{
if (!instance.configuration.getString("Mode").equals("RPC"))
printPreamble(this.ConsoleReader);
while (true)
{
String command = this.ConsoleReader.readLine("\r> ", null);
// The user sent CTRL+D to close the shell, terminate the session.
if (command == null)
break;
// Skip someone spamming enter
if (command.trim().isEmpty())
continue;
// User wants to exit
if (command.equals("exit") || command.equals("quit"))
break;
// Clear the text from the screen (on supported terminals)
if (command.equals("cls"))
{
this.ConsoleReader.clearScreen();
continue;
}
// Hide the mkpasswd command input from other users.
Boolean mkpasswd = command.split(" ")[0].equals("mkpasswd");
instance.getProxy().getScheduler().runAsync(
instance, () ->
{
if (instance.configuration.getString("Mode").equals("RPC") && command.startsWith("rpc"))
{
// NO ECHO NO PREAMBLE AND SHIT
String cmd = command.substring("rpc".length() + 1, command.length());
if (!instance.getProxy().getPluginManager().dispatchCommand(this.SshdCommandSender, cmd))
instance.getProxy().getConsole().sendMessage(new ComponentBuilder("Command not found").color(ChatColor.RED).create());
}
else
{
if (!mkpasswd)
instance.getLogger().info("<" + this.Username + "> " + command);
if (!instance.getProxy().getPluginManager().dispatchCommand(this.SshdCommandSender, command))
instance.getProxy().getConsole().sendMessage(new ComponentBuilder("Command not found").color(ChatColor.RED).create());
}
});
}
}
catch (IOException e)
{
e.printStackTrace();
instance.getLogger().log(Level.SEVERE, "Error processing command from SSH", e);
}
finally
{
instance.getLogger().log(Level.INFO, this.Username + " disconnected from SSH.");
callback.onExit(0);
}
}
private void printPreamble(ConsoleReader cr) throws IOException
{
File f = new File(instance.getDataFolder(), "motd.txt");
try
{
BufferedReader br = new BufferedReader(new FileReader(f));
String st;
while ((st = br.readLine()) != null)
cr.println(ConsoleLogFormatter.ColorizeString(st) + "\r");
}
catch (FileNotFoundException e)
{
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() + " (BungeeCord)\r");
// Since BungeeCord is stupid, we have to parse the config file and the the MOTD from it that way...
// If you try to use the method getMotd() it returns that it can't be referenced from a non-static context, which is stupid.
cr.println(ConsoleLogFormatter.ColorizeString(instance.configuration.getString("motd")).replaceAll("\n", "\r\n"));
cr.println("\r");
cr.println("Type 'exit' or press Ctrl+D exit the shell." + "\r");
cr.println("===============================================" + "\r");
}
}
}

View File

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

View File

@ -2,6 +2,7 @@ package com.ryanmichela.sshd;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;
import org.apache.sshd.common.SshException; import org.apache.sshd.common.SshException;
import org.apache.sshd.common.channel.exception.SshChannelClosedException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -40,20 +41,19 @@ public class FlushyStreamHandler extends StreamHandler
try try
{ {
reader.drawLine(); reader.drawLine();
} }
catch (Throwable ex) catch (Throwable ex)
{ {
reader.getCursorBuffer().clear(); reader.getCursorBuffer().clear();
} }
reader.flush(); reader.flush();
super.flush(); super.flush();
} }
catch (SshException ex) catch (SshChannelClosedException ex)
{ {
// do nothing // do nothing
} }
catch (IOException ex) catch (IOException ex)
{ {
Logger.getLogger(FlushyStreamHandler.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(FlushyStreamHandler.class.getName()).log(Level.SEVERE, null, ex);
} }

View File

@ -0,0 +1,85 @@
package com.ryanmichela.sshd;
import java.util.Arrays;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import com.ryanmichela.sshd.Cryptography;
import com.ryanmichela.sshd.SshdPlugin;
public class MkpasswdCommand extends Command
{
public MkpasswdCommand()
{
super("mkpasswd");
}
public void SendSyntax(CommandSender sender, boolean invalid)
{
if (invalid)
sender.sendMessage(new ComponentBuilder("Invalid Syntax").color(ChatColor.RED).create());
sender.sendMessage(new ComponentBuilder("/mkpasswd <help|hash> <password>").color(ChatColor.GREEN).create());
sender.sendMessage(new ComponentBuilder("Supported Hashes: SHA256, PBKDF2, BCRYPT, PLAIN").color(ChatColor.BLUE).create());
}
@Override
public void execute(CommandSender sender, String[] args)
{
String algoritm, password;
try
{
// Stupid bukkit, we have to concatenate the arguments together if they're using
// spaces in their passwords otherwise it won't be as strong as it should be.
algoritm = args[0];
password = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
if (password.length() == 0)
throw new ArrayIndexOutOfBoundsException(); // shortcut
}
catch (ArrayIndexOutOfBoundsException e)
{
this.SendSyntax(sender, false);
return;
}
// If they're a player, check and make sure they have a permission
// If they're not a player (aka, the console), just return true.
boolean hasperm = (sender instanceof ProxiedPlayer) ? ((ProxiedPlayer)sender).hasPermission("sshd.mkpasswd") : true;
if (hasperm)
{
try
{
String hash = "";
// Dumb but whatever. Some people are really dense.
if (algoritm.equalsIgnoreCase("PLAIN"))
{
// I mean c'mon...
sender.sendMessage("\u00A79Your Hash: \u00A7cIt's literally your unhashed password.");
return;
}
else if (algoritm.equalsIgnoreCase("pbkdf2"))
hash = Cryptography.PBKDF2_HashPassword(password);
else if (algoritm.equalsIgnoreCase("bcrypt"))
hash = Cryptography.BCrypt_HashPassword(password);
else if (algoritm.equalsIgnoreCase("sha256"))
hash = Cryptography.SHA256_HashPassword(password);
else
{
this.SendSyntax(sender, !algoritm.equalsIgnoreCase("help"));
return;
}
sender.sendMessage(new ComponentBuilder("Your Hash: " + hash).color(ChatColor.BLUE).create());
}
catch (Exception e)
{
// We're console, just print the stack trace.
e.printStackTrace();
}
}
}
}

View File

@ -1,5 +1,7 @@
package com.ryanmichela.sshd; package com.ryanmichela.sshd;
import net.md_5.bungee.config.Configuration;
import org.apache.commons.lang.ArrayUtils;
import org.apache.sshd.common.config.keys.AuthorizedKeyEntry; import org.apache.sshd.common.config.keys.AuthorizedKeyEntry;
import org.apache.sshd.common.config.keys.PublicKeyEntryResolver; import org.apache.sshd.common.config.keys.PublicKeyEntryResolver;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator; import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
@ -19,14 +21,13 @@ public class PublicKeyAuthenticator implements PublickeyAuthenticator
{ {
private File authorizedKeysDir; private File authorizedKeysDir;
private Map<String, Integer> FailCounts = new HashMap<String, Integer>(); private Map<String, Integer> FailCounts = new HashMap<String, Integer>();
public PublicKeyAuthenticator(File authorizedKeysDir) { this.authorizedKeysDir = authorizedKeysDir; } public PublicKeyAuthenticator(File authorizedKeysDir) { this.authorizedKeysDir = authorizedKeysDir; }
@Override public boolean authenticate(String username, PublicKey key, ServerSession session) @Override public boolean authenticate(String username, PublicKey key, ServerSession session)
{ {
byte[] keyBytes = key.getEncoded(); byte[] keyBytes = key.getEncoded();
File keyFile = new File(authorizedKeysDir, username); File keyFile = new File(authorizedKeysDir, username);
Integer tries = SshdPlugin.instance.getConfig().getInt("LoginRetries", 3); Integer tries = SshdPlugin.instance.configuration.getInt("LoginRetries");
if (keyFile.exists()) if (keyFile.exists())
{ {
@ -67,8 +68,7 @@ public class PublicKeyAuthenticator implements PublickeyAuthenticator
} }
catch (Exception e) catch (Exception e)
{ {
e.printStackTrace(); SshdPlugin.instance.getLogger().severe("Failed to process public key " + keyFile.getAbsolutePath() + " " + e.getMessage());
SshdPlugin.instance.getLogger().severe("Failed to process public key " + keyFile.getAbsolutePath());
} }
} }
else else

View File

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

View File

@ -0,0 +1,134 @@
package com.ryanmichela.sshd;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
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 net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.config.*;
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 final class SshdPlugin extends Plugin
{
private SshServer sshd;
public static SshdPlugin instance;
private File file;
public Configuration configuration;
@Override public void onLoad()
{
file = new File(getDataFolder(), "config.yml");
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();
}
try
{
if (!file.exists())
{
// Copy our config file.
InputStream link = (getClass().getResourceAsStream("/config.yml"));
Files.copy(link, file.getAbsoluteFile().toPath());
}
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
}
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(configuration.getInt("Port", 1025));
String host = configuration.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 (configuration.getBoolean("EnableSFTP"))
{
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
sshd.setFileSystemFactory(
new VirtualFileSystemFactory(FileSystems.getDefault().getPath(getDataFolder().getAbsolutePath()).getParent().getParent()));
}
getProxy().getPluginManager().registerCommand(this, 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
}
}
public static SshdPlugin getInstance() {
return instance;
}
private static void setInstance(SshdPlugin instance) {
SshdPlugin.instance = instance;
}
}

View File

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

View File

@ -0,0 +1,114 @@
package com.ryanmichela.sshd.implementations;
import com.ryanmichela.sshd.SshdPlugin;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import com.ryanmichela.sshd.ConsoleShellFactory;
import com.ryanmichela.sshd.ConsoleLogFormatter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
public final class SSHDCommandSender implements CommandSender
{
public ConsoleShellFactory.ConsoleShell console;
@Override
public void sendMessage(String message)
{
this.sendRawMessage(message + "\r");
}
public void sendRawMessage(String message)
{
if (this.console.ConsoleReader == null)
return;
try
{
this.console.ConsoleReader.println(ConsoleLogFormatter.ColorizeString(message).replace("\n", "\n\r"));
this.console.ConsoleReader.print(this.console.ConsoleReader.RESET_LINE + "");
this.console.ConsoleReader.flush();
try
{
this.console.ConsoleReader.drawLine();
}
catch (Throwable ex)
{
this.console.ConsoleReader.getCursorBuffer().clear();
}
this.console.ConsoleReader.flush();
}
catch (IOException e)
{
SshdPlugin.instance.getLogger().log(Level.SEVERE, "Error sending message to SSHDCommandSender", e);
}
}
@Override
public void sendMessages(String... messages)
{
Arrays.asList(messages).forEach(this::sendMessage);
}
@Override
public void sendMessage(BaseComponent... message)
{
sendMessage(BaseComponent.toLegacyText(message));
}
@Override
public void sendMessage(BaseComponent message)
{
sendMessage(message.toLegacyText());
}
@Override
public String getName()
{
return "SSHD CONSOLE";
}
@Override
public Collection<String> getGroups()
{
return Collections.emptySet();
}
@Override
public void addGroups(String... groups)
{
throw new UnsupportedOperationException("Console may not have groups");
}
@Override
public void removeGroups(String... groups)
{
throw new UnsupportedOperationException("Console may not have groups");
}
@Override
public boolean hasPermission(String permission)
{
return true;
}
@Override
public void setPermission(String permission, boolean value)
{
throw new UnsupportedOperationException("Console has all permissions");
}
@Override
public Collection<String> getPermissions()
{
return Collections.emptySet();
}
}

View File

@ -0,0 +1,41 @@
# 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

View File

@ -2,8 +2,7 @@ name: SSHD
version: ${project.version} version: ${project.version}
author: Ryan Michela, Haarolean, toxuin, Justin Crawford, Zachery Coleman author: Ryan Michela, Haarolean, toxuin, Justin Crawford, Zachery Coleman
main: com.ryanmichela.sshd.SshdPlugin main: com.ryanmichela.sshd.SshdPlugin
load: STARTUP
commands: commands:
mkpasswd: mkpasswd:
description: Make a SSHD password hash description: Make a SSHD password hash
usage: /mkpasswd <hash|help> <password> usage: /mkpasswd <hash|help> <password>