Lock down various commands while offline.

This commit is contained in:
Michael 2017-03-11 09:04:45 -05:00
parent b9c3a0e30f
commit 49812bf46c
4 changed files with 172 additions and 123 deletions

View file

@ -351,6 +351,7 @@ namespace ShiftOS.WinForms
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
[MultiplayerOnly]
[Command("breach_user_password")]
[KernelMode]
[RequiresArgument("user")]
@ -413,6 +414,8 @@ namespace ShiftOS.WinForms
return true;
}
[MultiplayerOnly]
[Command("print_user_info")]
[KernelMode]
[RequiresArgument("pass")]
@ -475,6 +478,7 @@ namespace ShiftOS.WinForms
return true;
}
[MultiplayerOnly]
[Command("steal_codepoints")]
[KernelMode]
[RequiresArgument("amount")]
@ -551,6 +555,7 @@ namespace ShiftOS.WinForms
return true;
}
[MultiplayerOnly]
[Command("purge_user")]
[KernelMode]
[RequiresArgument("pass")]
@ -646,6 +651,7 @@ namespace ShiftOS.WinForms
}
}
[MultiplayerOnly]
[Namespace("storydev")]
public static class StoryDevCommands
{

View file

@ -133,6 +133,7 @@ namespace ShiftOS.Engine
[Namespace("mud")]
public static class MUDCommands
{
[MultiplayerOnly]
[Command("status")]
public static bool Status()
{
@ -174,6 +175,7 @@ namespace ShiftOS.Engine
return true;
}
[MultiplayerOnly]
[Command("disconnect")]
[RequiresUpgrade("hacker101_deadaccts")]
public static bool Disconnect()
@ -183,6 +185,7 @@ namespace ShiftOS.Engine
return true;
}
[MultiplayerOnly]
[Command("sendmsg")]
[KernelMode]
[RequiresUpgrade("hacker101_deadaccts")]
@ -195,48 +198,6 @@ namespace ShiftOS.Engine
}
}
[RequiresUpgrade("mud_fundamentals")]
[Namespace("chat")]
public static class ChatCommands
{
[RequiresArgument("id")]
[RequiresArgument("name")]
[RequiresArgument("topic")]
[Command("create")]
public static bool CreateChat(Dictionary<string, object> args)
{
string id = "";
string topic = "";
string name = "";
int max_users = 0;
id = args["id"] as string;
name = args["topic"] as string;
topic = args["name"] as string;
bool valid = true;
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(topic))
valid = false;
if (valid)
{
ServerManager.SendMessage("chat_create", $@"{{
id: ""{id}"",
name: ""{name}"",
topic: ""{topic}"",
max_users: {max_users}
}}");
}
else
{
Console.WriteLine("{CHAT_PLEASE_PROVIDE_VALID_CHANNEL_DATA}");
}
return true;
}
}
[TutorialLock]
[Namespace("trm")]
public static class TerminalCommands
@ -266,6 +227,7 @@ namespace ShiftOS.Engine
}
}
[MultiplayerOnly]
[Namespace("dev")]
public static class ShiftOSDevCommands
{
@ -474,6 +436,7 @@ namespace ShiftOS.Engine
return true;
}
[MultiplayerOnly]
[Command("save")]
public static bool Save()
{
@ -481,6 +444,7 @@ namespace ShiftOS.Engine
return true;
}
[MultiplayerOnly]
[Command("status")]
public static bool Status()
{
@ -493,7 +457,7 @@ Upgrades: {SaveSystem.CurrentSave.CountUpgrades()} installed,
}
}
[MultiplayerOnly]
[Namespace("shiftorium")]
public static class ShiftoriumCommands
{

View file

@ -102,5 +102,31 @@ namespace ShiftOS.Engine
InKernelMode = false;
Console.WriteLine("<kernel> Kernel mode disabled.");
}
internal static bool CanRunOffline(Type method)
{
if (MudConnected)
return true;
foreach (var attr in method.GetCustomAttributes(false))
{
if (attr is MultiplayerOnlyAttribute)
return false;
}
return true;
}
internal static bool CanRunOffline(MethodInfo method)
{
if (MudConnected)
return true;
foreach(var attr in method.GetCustomAttributes(false))
{
if (attr is MultiplayerOnlyAttribute)
return false;
}
return true;
}
}
}

View file

@ -148,128 +148,181 @@ namespace ShiftOS.Engine
{
if (KernelWatchdog.IsSafe(type))
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
if (KernelWatchdog.CanRunOffline(type))
{
if (Shiftorium.UpgradeAttributesUnlocked(method))
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
if (CanRunRemotely(method, isRemote))
if (Shiftorium.UpgradeAttributesUnlocked(method))
{
foreach (var ma in method.GetCustomAttributes(false))
if (CanRunRemotely(method, isRemote))
{
if (ma is Command)
foreach (var ma in method.GetCustomAttributes(false))
{
var cmd = ma as Command;
if (text.Split('.')[1] == cmd.name)
if (ma is Command)
{
if (KernelWatchdog.IsSafe(method))
var cmd = ma as Command;
if (text.Split('.')[1] == cmd.name)
{
var attr = method.GetCustomAttribute<CommandObsolete>();
if (attr != null)
if (KernelWatchdog.IsSafe(method))
{
string newcommand = attr.newcommand;
if (attr.warn)
if (KernelWatchdog.CanRunOffline(method))
{
Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary<string, string>() {
var attr = method.GetCustomAttribute<CommandObsolete>();
if (attr != null)
{
string newcommand = attr.newcommand;
if (attr.warn)
{
Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary<string, string>() {
{"%newcommand", newcommand}
}));
}
if (newcommand != "")
{
// redo the entire process running newcommand
}
if (newcommand != "")
{
// redo the entire process running newcommand
return RunClient(newcommand, args);
}
}
return RunClient(newcommand, args);
}
}
var requiresArgs = method.GetCustomAttributes<RequiresArgument>();
var requiresArgs = method.GetCustomAttributes<RequiresArgument>();
bool error = false;
bool providedusage = false;
bool error = false;
bool providedusage = false;
foreach (RequiresArgument argument in requiresArgs)
{
if (!args.ContainsKey(argument.argument))
{
if (!providedusage)
foreach (RequiresArgument argument in requiresArgs)
{
string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}";
if (usageparse == Localization.Parse(usageparse))
usageparse = "";
else
usageparse = Shiftorium.UpgradeInstalled("help_usage") ? Localization.Parse("{ERROR}{USAGE}" + usageparse, new Dictionary<string, string>() {
if (!args.ContainsKey(argument.argument))
{
if (!providedusage)
{
string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}";
if (usageparse == Localization.Parse(usageparse))
usageparse = "";
else
usageparse = Shiftorium.UpgradeInstalled("help_usage") ? Localization.Parse("{ERROR}{USAGE}" + usageparse, new Dictionary<string, string>() {
{"%ns", ns.name},
{"%cmd", cmd.name}
}) : "";
Console.WriteLine(usageparse);
Console.WriteLine(usageparse);
providedusage = true;
}
providedusage = true;
}
if (Shiftorium.UpgradeInstalled("help_usage"))
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary<string, string>() {
if (Shiftorium.UpgradeInstalled("help_usage"))
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary<string, string>() {
{"%argument", argument.argument}
}));
}
else
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}"));
}
else
{
Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}"));
}
error = true;
}
}
error = true;
if (error)
{
throw new Exception("{ERROR_COMMAND_WRONG}");
}
try
{
return (bool)method.Invoke(null, new[] { args });
}
catch (TargetInvocationException e)
{
Console.WriteLine(Localization.Parse("{ERROR_EXCEPTION_THROWN_IN_METHOD}"));
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
return true;
}
catch
{
return (bool)method.Invoke(null, new object[] { });
}
}
else
{
Console.Write("<");
ConsoleEx.Bold = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("session_mgr");
ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC;
ConsoleEx.Bold = false;
Console.Write(">");
ConsoleEx.Italic = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" You cannot run this command while disconnected from the multi-user domain..");
return true;
}
}
if (error)
else
{
throw new Exception("{ERROR_COMMAND_WRONG}");
}
try
{
return (bool)method.Invoke(null, new[] { args });
}
catch (TargetInvocationException e)
{
Console.WriteLine(Localization.Parse("{ERROR_EXCEPTION_THROWN_IN_METHOD}"));
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
Console.Write("<");
ConsoleEx.Bold = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("watchdog");
ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC;
ConsoleEx.Bold = false;
Console.Write(">");
ConsoleEx.Italic = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" You cannot run this command.");
KernelWatchdog.Log("potential_sys_breach", "user attempted to run kernel mode command " + text + " - watchdog has prevented this, good sir.");
return true;
}
catch
{
return (bool)method.Invoke(null, new object[] { });
}
}
else
{
Console.WriteLine("<watchdog> You cannot run this command.");
KernelWatchdog.Log("potential_sys_breach", "user attempted to run kernel mode command " + text + " - watchdog has prevented this, good sir.");
return true;
}
}
}
}
else
{
Console.WriteLine(text + " cannot be ran in a remote session");
return true;
}
}
else
{
Console.WriteLine(text + " cannot be ran in a remote session");
return true;
}
}
}
else
{
Console.Write("<");
ConsoleEx.Bold = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("session_mgr");
ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC;
ConsoleEx.Bold = false;
Console.Write(">");
ConsoleEx.Italic = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" You cannot run this command while disconnected from the multi-user domain..");
return true;
}
}
else
{
Console.WriteLine("<watchdog> You cannot run this command.");
Console.Write("<");
ConsoleEx.Bold = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("watchdog");
ConsoleEx.ForegroundColor = SkinEngine.LoadedSkin.TerminalForeColorCC;
ConsoleEx.Bold = false;
Console.Write(">");
ConsoleEx.Italic = true;
ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" You cannot run this command.");
KernelWatchdog.Log("potential_sys_breach", "user attempted to run kernel mode command " + text + " - watchdog has prevented this, good sir.");
return true;
}