Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -PWRESET Command Line Argument #603

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 84 additions & 43 deletions MBBSEmu/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class Program
/// </summary>
private bool _doResetDatabase;

/// <summary>
/// Specified if -PWRESET Command Line Argument was Passed
/// </summary>
private bool _doResetPassword;

/// <summary>
/// New Sysop Password specified by the -DBRESET Command Line Argument
/// </summary>
Expand Down Expand Up @@ -254,6 +259,17 @@ private void Run(string[] args)
_isConsoleSession = true;
break;
}
case "-PWRESET":
{
_doResetPassword = true;
if (i + 1 < args.Length && args[i + 1][0] != '-')
{
_newSysopPassword = args[i + 1];
i++;
}

break;
}
default:
Console.WriteLine($"Unknown Command Line Argument: {args[i]}");
return;
Expand Down Expand Up @@ -295,28 +311,6 @@ private void Run(string[] args)
return;
}

//If the user specified CLI mode, don't start the UI
if (!_cliMode)
{
var mainMBBSEmuWindow = new MainView(_serviceResolver);

//Start UI in a Task as to not block this thread
Task.Run(() =>
{
mainMBBSEmuWindow.Setup();
mainMBBSEmuWindow.Run();
});

//Wait for the UI to be running before continuing
Console.Write("Waiting for MBBSEmu GUI to start...");
while (!mainMBBSEmuWindow.isRunning)
{
Thread.Sleep(500);
Console.Write(".");
}
Console.WriteLine();
}

var configuration = _serviceResolver.GetService<AppSettingsManager>();
var textVariableService = _serviceResolver.GetService<ITextVariableService>();
var resourceManager = _serviceResolver.GetService<IResourceManager>();
Expand All @@ -343,6 +337,10 @@ private void Run(string[] args)
if (_doResetDatabase)
DatabaseReset();

//Password Reset
if (_doResetPassword)
PasswordReset();

//Database Sanity Checks
var databaseFile = configuration.DatabaseFile;
if (!File.Exists($"{databaseFile}"))
Expand Down Expand Up @@ -433,6 +431,28 @@ private void Run(string[] args)
return;
}

//If the user specified CLI mode, don't start the UI
if (!_cliMode && !_isConsoleSession)
{
var mainMBBSEmuWindow = new MainView(_serviceResolver);

//Start UI in a Task as to not block this thread
Task.Run(() =>
{
mainMBBSEmuWindow.Setup();
mainMBBSEmuWindow.Run();
});

//Wait for the UI to be running before continuing
Console.Write("Waiting for MBBSEmu GUI to start...");
while (!mainMBBSEmuWindow.isRunning)
{
Thread.Sleep(500);
Console.Write(".");
}
Console.WriteLine();
}

//Setup and Run Host
var host = _serviceResolver.GetService<IMbbsHost>();
host.Start(_moduleConfigurations);
Expand Down Expand Up @@ -519,7 +539,7 @@ private void CancelKeyPressHandler(object sender, ConsoleCancelEventArgs args)

_cancellationRequests++;

_logger.Warn("BBS Shutting down");
_logger.Warn("MBBSEmu Shutting down...");

foreach (var runningService in _runningServices)
{
Expand All @@ -536,27 +556,8 @@ private void DatabaseReset()
{
_logger.Info("Resetting Database...");


if (string.IsNullOrEmpty(_newSysopPassword))
{
var bPasswordMatch = false;
while (!bPasswordMatch)
{
Console.Write("Enter New Sysop Password: ");
var password1 = Console.ReadLine();
Console.Write("Re-Enter New Sysop Password: ");
var password2 = Console.ReadLine();
if (password1 == password2)
{
bPasswordMatch = true;
_newSysopPassword = password1;
}
else
{
Console.WriteLine("Password mismatch, please try again.");
}
}
}
_newSysopPassword = PasswordPrompt("sysop");

var acct = _serviceResolver.GetService<IAccountRepository>();
acct.Reset(_newSysopPassword);
Expand All @@ -576,5 +577,45 @@ private void DatabaseReset()

_logger.Info("Database Reset!");
}

/// <summary>
/// Prompts the user via the CLI to enter a new password for the specified username
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private string PasswordPrompt(string username)
{
while (true)
{
Console.Write($"Enter New Password for {username}: ");
var password1 = Console.ReadLine();
Console.Write($"Re-Enter New Password for {username}: ");
var password2 = Console.ReadLine();

//If they match, return the password
if (password1 == password2)
return password1;

//Otherwise infinite loop until they match
Console.WriteLine("Password mismatch, please try again.");
}
}

/// <summary>
/// Reset the Sysop Password to the specified value via the console
/// </summary>
private void PasswordReset()
{
//Interactive Password Reset via Console if one wasn't specified in the command line
if (string.IsNullOrEmpty(_newSysopPassword))
_newSysopPassword = PasswordPrompt("sysop");

var acct = _serviceResolver.GetService<IAccountRepository>();
var sysopAccount = acct.GetAccountByUsername("sysop");
acct.UpdateAccountById(sysopAccount.accountId, sysopAccount.userName, _newSysopPassword, sysopAccount.email);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we clear the _newSysopPassword reference for security purposes here?

_logger.Info("Sysop Password Reset!");
_doResetPassword = false;
}
}
}
Loading