Skip to content

Commit

Permalink
Remove remnants of old /pass code
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jul 2, 2021
1 parent bccab6b commit 26e2c09
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 102 deletions.
89 changes: 0 additions & 89 deletions MCGalaxy/Commands/Moderation/CmdPass.cs
Expand Up @@ -52,14 +52,6 @@ public sealed class CmdPass : Command2 {
VerifyPassword(p, message);
}
}

static void StorePassword(string curPath, string name, string pass) {
byte[] hash = ComputeNewHash(name, pass);

// In case was using .dat password before
if (curPath != null) File.Delete(curPath);
File.WriteAllBytes(NewHashPath(name), hash);
}

static void VerifyPassword(Player p, string password) {
if (!p.Unverified) { p.Message("&WYou are already verified."); return; }
Expand Down Expand Up @@ -113,87 +105,6 @@ public sealed class CmdPass : Command2 {
p.Message("{0} &Sdoes not have a password.", p.FormatNick(target));
}
}


static byte[] ComputeOldHash(string name, string pass) {
// Pointless, but kept for backwards compatibility
pass = pass.Replace("<", "(");
pass = pass.Replace(">", ")");

MD5 hash = MD5.Create();
byte[] nameB = hash.ComputeHash(Encoding.ASCII.GetBytes(name));
// This line means that non-ASCII characters in passwords are
// all encoded as the "?" character.
byte[] dataB = hash.ComputeHash(Encoding.ASCII.GetBytes(pass));

byte[] result = new byte[nameB.Length + dataB.Length];
Array.Copy(nameB, 0, result, 0, nameB.Length);
Array.Copy(dataB, 0, result, nameB.Length, dataB.Length);
return hash.ComputeHash(result);
}

static byte[] ComputeNewHash(string name, string pass) {
// The constant string added to the username salt is to mitigate
// rainbox tables. We should really have a unique salt for each
// user, but this is close enough.
byte[] data = Encoding.UTF8.GetBytes("0bec662b-416f-450c-8f50-664fd4a41d49" + name.ToLower() + " " + pass);
return SHA256.Create().ComputeHash(data);
}

static bool ArraysEqual(byte[] a, byte[] b) {
if (a.Length != b.Length) return false;

for (int i = 0; i < a.Length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}

static bool CheckNewHash(string path, string name, string pass) {
byte[] stored = File.ReadAllBytes(path);
byte[] computed = ComputeNewHash(name, pass);
return ArraysEqual(computed, stored);
}

static bool CheckOldHash(string path, string name, string pass) {
byte[] stored = File.ReadAllBytes(path);
byte[] computed = ComputeOldHash(name, pass);

// Old passwords stored UTF8 string instead of just the raw 16 byte hashes
// We need to support both since this behaviour was accidentally changed
if (stored.Length != computed.Length) {
return Encoding.UTF8.GetString(stored) == Encoding.UTF8.GetString(computed);
}
return ArraysEqual(computed, stored);
}


static string NewHashPath(string name) {
// don't want '+' at end of names
return passDir + name.RemoveLastPlus().ToLower() + ".pwd";
}

static string FindOldHashPath(string name) {
string path = passDir + name + ".dat";
if (File.Exists(path)) return path;

// Have to fallback on this for case sensitive file systems
string[] files = AtomicIO.TryGetFiles(passDir, "*.dat");
if (files == null) return null;

foreach (string file in files) {
if (file.CaselessEq(path)) return file;
}
return null;
}

static string FindHashPath(string name) {
string path = NewHashPath(name);
if (File.Exists(path)) return path;
return FindOldHashPath(name);
}

public static bool HasPassword(string name) { return FindHashPath(name) != null; }

public override void Help(Player p) {
p.Message("&T/Pass reset [player] &H- Resets the password for that player");
Expand Down
24 changes: 21 additions & 3 deletions MCGalaxy/Network/Listeners.cs
Expand Up @@ -47,8 +47,25 @@ public sealed class TcpListen : INetListen {
try {
socket.SetSocketOption(SocketOptionLevel.IPv6, ipv6Only, false);
} catch (Exception ex) {
Logger.LogError(ex);
Logger.Log(LogType.Warning, "Failed to disable IPv6 only listener setting");
Logger.LogError("Failed to disable IPv6 only listener setting", ex);
}
}

void EnableAddressReuse() {
// https://stackoverflow.com/questions/3229860/what-is-the-meaning-of-so-reuseaddr-setsockopt-option-linux
// http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-4.html#ss4.2
// https://docs.microsoft.com/en-us/windows/win32/winsock/graceful-shutdown-linger-options-and-socket-closure-2
// https://stackoverflow.com/questions/45535077/address-in-use-on-tcp-socket-bind
// https://stackoverflow.com/questions/3757289/when-is-tcp-option-so-linger-0-required
// https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states
// http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html
// https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states
// https://stackoverflow.com/questions/35322550/is-there-a-way-to-enable-the-so-reuseaddr-socket-option-when-using-system-net-ht
try {
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
} catch {
// not really a critical issue if this fails to work
// TODO only do this on Mono
}
}

Expand All @@ -60,8 +77,9 @@ public sealed class TcpListen : INetListen {
try {
socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
DisableIPV6OnlyListener();
socket.Bind(new IPEndPoint(ip, port));
EnableAddressReuse();

socket.Bind(new IPEndPoint(ip, port));
socket.Listen((int)SocketOptionName.MaxConnections);
AcceptNextAsync();
} catch (Exception ex) {
Expand Down
6 changes: 1 addition & 5 deletions MCGalaxy/Player/Player.cs
Expand Up @@ -373,11 +373,7 @@ public partial class Player : Entity, IDisposable {
Unverified = Server.Config.verifyadmins && Rank >= Server.Config.VerifyAdminsRank;
if (!Unverified) return;

if (!Commands.Moderation.CmdPass.HasPassword(name)) {
Message("&WPlease set your admin verification password with &T/SetPass [password]!");
} else {
Message("&WPlease complete admin verification with &T/Pass [password]!");
}
Authenticator.Current.NeedVerification(this);
}


Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Server/Authenticator.cs
Expand Up @@ -52,7 +52,7 @@ public abstract class Authenticator {
/// <summary> Informs the given player that they should verify,
/// otherwise they will be unable to perform some actions </summary>
public virtual void NeedVerification(Player p) {
if (!Commands.Moderation.CmdPass.HasPassword(p.name)) {
if (!HasPassword(p.name)) {
p.Message("&WPlease set your admin verification password with &T/SetPass [password]!");
} else {
p.Message("&WPlease complete admin verification with &T/Pass [password]!");
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Server/Logger.cs
Expand Up @@ -164,7 +164,7 @@ public static class Logger {
}

static void LogSocketErrors(SocketException ex, StringBuilder sb) {
sb.AppendLine("Error: " + ex.SocketErrorCode + " (" + ex.NativeErrorCode + ")");
sb.AppendLine("Error: " + ex.SocketErrorCode);
}
}
}
4 changes: 1 addition & 3 deletions MCGalaxy/Server/Server.cs
Expand Up @@ -265,7 +265,7 @@ public sealed partial class Server {

if (restarting) {
// first try to use excevp to restart in CLI mode under mono
// - see detailed comment in Excvp_Hack for why this is required
// - see detailed comment in HACK_Execvp for why this is required
if (HACK_TryExecvp()) HACK_Execvp();
Process.Start(RestartPath);
}
Expand All @@ -285,9 +285,7 @@ public sealed partial class Server {
// is called, all FDs (including standard input) are also closed.
// Unfortunately, this causes the new server process to constantly error with
// Type: IOException
// Source: mscorlib
// Message: Invalid handle to path "server_folder_path/[Unknown]"
// Target: ReadData
// Trace: at System.IO.FileStream.ReadData (System.Runtime.InteropServices.SafeHandle safeHandle, System.Byte[] buf, System.Int32 offset, System.Int32 count) [0x0002d]
// at System.IO.FileStream.ReadInternal (System.Byte[] dest, System.Int32 offset, System.Int32 count) [0x00026]
// at System.IO.FileStream.Read (System.Byte[] array, System.Int32 offset, System.Int32 count) [0x000a1]
Expand Down

0 comments on commit 26e2c09

Please sign in to comment.