Skip to content

Commit

Permalink
add /AUTH command and -chatCommandPassword command line switch so…
Browse files Browse the repository at this point in the history
… that users can also authenticate via password
  • Loading branch information
derhass committed Feb 28, 2022
1 parent 09dd4b0 commit bba0611
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions GameMod/MPChatCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public enum Command {
// List of Commands
GivePerm,
RevokePerm,
Auth,
Kick,
Ban,
KickBan,
Expand Down Expand Up @@ -153,6 +154,9 @@ public MPChatCommand(string message, int sender_connection_id, bool isInLobby) {
} else if (cmdName == "RP" || cmdName == "REVOKEPERM") {
cmd = Command.RevokePerm;
needAuth = true;
} else if (cmdName == "AUTH") {
cmd = Command.Auth;
needAuth = false;
} else if (cmdName == "K" || cmdName == "KICK") {
cmd = Command.Kick;
needAuth = true;
Expand Down Expand Up @@ -223,6 +227,9 @@ public bool Execute() {
case Command.RevokePerm:
result = DoPerm(false);
break;
case Command.Auth:
result = DoAuth();
break;
case Command.Kick:
result = DoKickBan(true, false, MPBanMode.Ban);
break;
Expand Down Expand Up @@ -268,7 +275,7 @@ private static void GetTrustedPlayerIds() {
}
trustedPlayers = new List<MPBanEntry>();
string idstring = null;
if (!GameMod.Core.GameMod.FindArgVal("-trustedPlayerIds", out idstring) && String.IsNullOrEmpty(idstring)) {
if (!GameMod.Core.GameMod.FindArgVal("-trustedPlayerIds", out idstring) || String.IsNullOrEmpty(idstring)) {
return; // no trustedPlayerIds specified;
}
string[] ids = idstring.Split(',',';',':','|');
Expand All @@ -293,7 +300,7 @@ private static void GetTrustedPlayerIds() {
private static bool SetAuth(bool allowed, MPBanEntry playerEntry)
{
if (playerEntry == null || !playerEntry.IsValid()) {
Debug.LogFormat("SETAUTH callid without valid player");
Debug.LogFormat("SETAUTH called without valid player");
return false;
}

Expand All @@ -319,6 +326,26 @@ private static bool SetAuth(bool allowed, MPBanEntry playerEntry)
return true;
}

// check if the supplied password matches the server password
private static bool SetAuthIsPassword(string password)
{
if (String.IsNullOrEmpty(password)) {
Debug.Log("SETAUTH Password check called without password");
return false;
}
string serverPassword = null;
if (!GameMod.Core.GameMod.FindArgVal("-chatCommandPassword", out serverPassword) || String.IsNullOrEmpty(serverPassword)) {
Debug.Log("SETAUTH Password is DISABLED on this server");
return false;
}
if (password.ToUpper() == serverPassword.ToUpper()) {
Debug.Log("SETAUTH Password CORRECT");
return true;
}
Debug.LogFormat("SETAUTH Password {0} is WRONG", password);
return false;
}

// Check if a player is an authenticated player on this server
public static bool IsAuthenticatedPlayer(MPBanEntry playerEntry)
{
Expand Down Expand Up @@ -378,6 +405,29 @@ public bool DoPerm(bool give)
return false;
}

// Execute the AUTH command
public bool DoAuth()
{
if (!SetAuthIsPassword(arg)) {
ReturnToSender("Nope.");
// De-Authenticate player with wrong password
SetAuth(false, senderEntry);
return false;
}

if (IsAuthenticatedPlayer(senderEntry)) {
ReturnToSender("Already Authenticated");
return false;
}
if (SetAuth(true, senderEntry)) {
ReturnToSender("AUTH successfull.");
} else {
// this shouldn't really happen...
ReturnToSender("AUTH failed! See server log for details.");
}
return false;
}

// Execute KICK or BAN or KICKBAN or ANNOY command
public bool DoKickBan(bool doKick, bool doBan, MPBanMode banMode) {
string op;
Expand Down

0 comments on commit bba0611

Please sign in to comment.