Missing files + start working on offline patcher

This commit is contained in:
2025-06-23 13:42:14 +02:00
parent 058d48196a
commit 2d36fecb45
70 changed files with 11475 additions and 12 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using SafeExamBrowser.Logging.Contracts;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class ApplicationsResponsibility : ClientResponsibility
{
public ApplicationsResponsibility(ClientContext context, ILogger logger) : base(context, logger)
{
}
public override void Assume(ClientTask task)
{
if (task == ClientTask.AutoStartApplications)
{
AutoStart();
}
}
private void AutoStart()
{
foreach (var application in Context.Applications)
{
if (application.AutoStart)
{
Logger.Info($"Auto-starting '{application.Name}'...");
application.Start();
}
}
}
}
}

View File

@@ -0,0 +1,212 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using SafeExamBrowser.Browser.Contracts;
using SafeExamBrowser.Browser.Contracts.Events;
using SafeExamBrowser.Client.Contracts;
using SafeExamBrowser.Communication.Contracts.Proxies;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Settings;
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Windows;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class BrowserResponsibility : ClientResponsibility
{
private readonly ICoordinator coordinator;
private readonly IMessageBox messageBox;
private readonly IRuntimeProxy runtime;
private readonly ISplashScreen splashScreen;
private readonly ITaskbar taskbar;
private IBrowserApplication Browser => Context.Browser;
public BrowserResponsibility(
ClientContext context,
ICoordinator coordinator,
ILogger logger,
IMessageBox messageBox,
IRuntimeProxy runtime,
ISplashScreen splashScreen,
ITaskbar taskbar) : base(context, logger)
{
this.coordinator = coordinator;
this.messageBox = messageBox;
this.runtime = runtime;
this.splashScreen = splashScreen;
this.taskbar = taskbar;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.AutoStartApplications:
AutoStartBrowser();
break;
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
}
}
private void AutoStartBrowser()
{
if (Settings.Browser.EnableBrowser && Browser.AutoStart)
{
Logger.Info("Auto-starting browser...");
Browser.Start();
}
}
private void DeregisterEvents()
{
if (Browser != default)
{
Browser.ConfigurationDownloadRequested -= Browser_ConfigurationDownloadRequested;
Browser.LoseFocusRequested -= Browser_LoseFocusRequested;
Browser.TerminationRequested -= Browser_TerminationRequested;
Browser.UserIdentifierDetected -= Browser_UserIdentifierDetected;
}
}
private void RegisterEvents()
{
Browser.ConfigurationDownloadRequested += Browser_ConfigurationDownloadRequested;
Browser.LoseFocusRequested += Browser_LoseFocusRequested;
Browser.TerminationRequested += Browser_TerminationRequested;
Browser.UserIdentifierDetected += Browser_UserIdentifierDetected;
}
private void Browser_ConfigurationDownloadRequested(string fileName, DownloadEventArgs args)
{
args.AllowDownload = false;
if (IsAllowedToReconfigure(args.Url))
{
if (coordinator.RequestReconfigurationLock())
{
args.AllowDownload = true;
args.Callback = Browser_ConfigurationDownloadFinished;
args.DownloadPath = Path.Combine(Context.AppConfig.TemporaryDirectory, fileName);
splashScreen.Show();
splashScreen.BringToForeground();
splashScreen.SetIndeterminate();
splashScreen.UpdateStatus(TextKey.OperationStatus_InitializeSession, true);
Logger.Info($"Allowed download request for configuration file '{fileName}'.");
}
else
{
Logger.Warn($"A reconfiguration is already in progress, denied download request for configuration file '{fileName}'!");
}
}
else
{
Logger.Info($"Reconfiguration is not allowed, denied download request for configuration file '{fileName}'.");
}
}
private bool IsAllowedToReconfigure(string url)
{
var allow = false;
var hasQuitPassword = !string.IsNullOrWhiteSpace(Settings.Security.QuitPasswordHash);
var hasUrl = !string.IsNullOrWhiteSpace(Settings.Security.ReconfigurationUrl);
if (hasQuitPassword)
{
if (hasUrl)
{
var expression = Regex.Escape(Settings.Security.ReconfigurationUrl).Replace(@"\*", ".*");
var regex = new Regex($"^{expression}$", RegexOptions.IgnoreCase);
var sebUrl = url.Replace(Uri.UriSchemeHttps, Context.AppConfig.SebUriSchemeSecure).Replace(Uri.UriSchemeHttp, Context.AppConfig.SebUriScheme);
allow = Settings.Security.AllowReconfiguration && (regex.IsMatch(url) || regex.IsMatch(sebUrl));
}
else
{
Logger.Warn("The active configuration does not contain a valid reconfiguration URL!");
}
}
else
{
allow = Settings.ConfigurationMode == ConfigurationMode.ConfigureClient || Settings.Security.AllowReconfiguration;
}
return allow;
}
private void Browser_ConfigurationDownloadFinished(bool success, string url, string filePath = null)
{
if (success)
{
PrepareShutdown();
var communication = runtime.RequestReconfiguration(filePath, url);
if (communication.Success)
{
Logger.Info($"Sent reconfiguration request for '{filePath}' to the runtime.");
}
else
{
Logger.Error($"Failed to communicate reconfiguration request for '{filePath}'!");
messageBox.Show(TextKey.MessageBox_ReconfigurationError, TextKey.MessageBox_ReconfigurationErrorTitle, icon: MessageBoxIcon.Error, parent: splashScreen);
splashScreen.Hide();
coordinator.ReleaseReconfigurationLock();
}
}
else
{
Logger.Error($"Failed to download configuration file '{filePath}'!");
messageBox.Show(TextKey.MessageBox_ConfigurationDownloadError, TextKey.MessageBox_ConfigurationDownloadErrorTitle, icon: MessageBoxIcon.Error, parent: splashScreen);
splashScreen.Hide();
coordinator.ReleaseReconfigurationLock();
}
}
private void Browser_LoseFocusRequested(bool forward)
{
taskbar.Focus(forward);
}
private void Browser_UserIdentifierDetected(string identifier)
{
if (Settings.SessionMode == SessionMode.Server)
{
var response = Context.Server.SendUserIdentifier(identifier);
while (!response.Success)
{
Logger.Error($"Failed to communicate user identifier with server! {response.Message}");
Thread.Sleep(Settings.Server.RequestAttemptInterval);
response = Context.Server.SendUserIdentifier(identifier);
}
}
}
private void Browser_TerminationRequested()
{
Logger.Info("Attempting to shutdown as requested by the browser...");
TryRequestShutdown();
}
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Collections.Generic;
using SafeExamBrowser.Core.Contracts.ResponsibilityModel;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Settings;
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
using SafeExamBrowser.UserInterface.Contracts.Windows.Data;
namespace SafeExamBrowser.Client.Responsibilities
{
internal abstract class ClientResponsibility : IResponsibility<ClientTask>
{
protected ClientContext Context { get; private set; }
protected ILogger Logger { get; private set; }
protected AppSettings Settings => Context.Settings;
internal ClientResponsibility(ClientContext context, ILogger logger)
{
Context = context;
Logger = logger;
}
public abstract void Assume(ClientTask task);
protected void PauseActivators()
{
foreach (var activator in Context.Activators)
{
activator.Pause();
}
}
protected void PrepareShutdown()
{
Context.Responsibilities.Delegate(ClientTask.PrepareShutdown_Wave1);
Context.Responsibilities.Delegate(ClientTask.PrepareShutdown_Wave2);
}
protected void ResumeActivators()
{
foreach (var activator in Context.Activators)
{
activator.Resume();
}
}
protected LockScreenResult ShowLockScreen(string message, string title, IEnumerable<LockScreenOption> options)
{
var hasQuitPassword = !string.IsNullOrEmpty(Settings.Security.QuitPasswordHash);
var result = default(LockScreenResult);
Context.LockScreen = Context.UserInterfaceFactory.CreateLockScreen(message, title, options, Settings.UserInterface.LockScreen);
Logger.Info("Showing lock screen...");
PauseActivators();
Context.LockScreen.Show();
if (Settings.SessionMode == SessionMode.Server)
{
var response = Context.Server.LockScreen(message);
if (!response.Success)
{
Logger.Error($"Failed to send lock screen notification to server! Message: {response.Message}.");
}
}
for (var unlocked = false; !unlocked;)
{
result = Context.LockScreen.WaitForResult();
if (result.Canceled)
{
Logger.Info("The lock screen has been automaticaly canceled.");
unlocked = true;
}
else if (hasQuitPassword)
{
var passwordHash = Context.HashAlgorithm.GenerateHashFor(result.Password);
var isCorrect = Settings.Security.QuitPasswordHash.Equals(passwordHash, StringComparison.OrdinalIgnoreCase);
if (isCorrect)
{
Logger.Info("The user entered the correct unlock password.");
unlocked = true;
}
else
{
Logger.Info("The user entered the wrong unlock password.");
Context.MessageBox.Show(TextKey.MessageBox_InvalidUnlockPassword, TextKey.MessageBox_InvalidUnlockPasswordTitle, icon: MessageBoxIcon.Warning, parent: Context.LockScreen);
}
}
else
{
Logger.Warn($"No unlock password is defined, allowing user to resume session!");
unlocked = true;
}
}
Context.LockScreen.Close();
ResumeActivators();
Logger.Info("Closed lock screen.");
if (Settings.SessionMode == SessionMode.Server)
{
var response = Context.Server.ConfirmLockScreen();
if (!response.Success)
{
Logger.Error($"Failed to send lock screen confirm notification to server! Message: {response.Message}.");
}
}
return result;
}
protected bool TryRequestShutdown()
{
PrepareShutdown();
var communication = Context.Runtime.RequestShutdown();
if (!communication.Success)
{
Logger.Error("Failed to communicate shutdown request to the runtime!");
Context.MessageBox.Show(TextKey.MessageBox_QuitError, TextKey.MessageBox_QuitErrorTitle, icon: MessageBoxIcon.Error);
}
return communication.Success;
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace SafeExamBrowser.Client.Responsibilities
{
/// <summary>
/// Defines all tasks assumed by the responsibilities of the client application.
/// </summary>
internal enum ClientTask
{
/// <summary>
/// Auto-start the browser and any potential third-party applications.
/// </summary>
AutoStartApplications,
/// <summary>
/// Close the shell.
/// </summary>
CloseShell,
/// <summary>
/// Deregister all event handlers during application termination.
/// </summary>
DeregisterEvents,
/// <summary>
/// Execute wave 1 of the application shutdown preparation. It should be used for potentially long-running operations which require all
/// other (security) functionalities to continue working normally.
/// </summary>
PrepareShutdown_Wave1,
/// <summary>
/// Execute wave 2 of the application shutdown preparation. It should be used by all remaining responsibilities which must continue to work
/// normally during the execution of wave 1.
/// </summary>
PrepareShutdown_Wave2,
/// <summary>
/// Register all event handlers during application initialization.
/// </summary>
RegisterEvents,
/// <summary>
/// Schedule the verification of the application integrity.
/// </summary>
ScheduleIntegrityVerification,
/// <summary>
/// Show the shell.
/// </summary>
ShowShell,
/// <summary>
/// Start the monitoring of different (security) aspects.
/// </summary>
StartMonitoring,
/// <summary>
/// Update the session integrity during application termination.
/// </summary>
UpdateSessionIntegrity,
/// <summary>
/// Verify the session integrity during application initialization.
/// </summary>
VerifySessionIntegrity
}
}

View File

@@ -0,0 +1,192 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Linq;
using SafeExamBrowser.Client.Contracts;
using SafeExamBrowser.Communication.Contracts.Data;
using SafeExamBrowser.Communication.Contracts.Events;
using SafeExamBrowser.Communication.Contracts.Hosts;
using SafeExamBrowser.Communication.Contracts.Proxies;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Server.Contracts.Data;
using SafeExamBrowser.UserInterface.Contracts;
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
using SafeExamBrowser.UserInterface.Contracts.Windows;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class CommunicationResponsibility : ClientResponsibility
{
private readonly ICoordinator coordinator;
private readonly IMessageBox messageBox;
private readonly IRuntimeProxy runtime;
private readonly Action shutdown;
private readonly ISplashScreen splashScreen;
private readonly IText text;
private readonly IUserInterfaceFactory uiFactory;
private IClientHost ClientHost => Context.ClientHost;
public CommunicationResponsibility(
ClientContext context,
ICoordinator coordinator,
ILogger logger,
IMessageBox messageBox,
IRuntimeProxy runtime,
Action shutdown,
ISplashScreen splashScreen,
IText text,
IUserInterfaceFactory uiFactory) : base(context, logger)
{
this.coordinator = coordinator;
this.messageBox = messageBox;
this.runtime = runtime;
this.shutdown = shutdown;
this.splashScreen = splashScreen;
this.text = text;
this.uiFactory = uiFactory;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
}
}
private void DeregisterEvents()
{
if (ClientHost != default)
{
ClientHost.ExamSelectionRequested -= ClientHost_ExamSelectionRequested;
ClientHost.MessageBoxRequested -= ClientHost_MessageBoxRequested;
ClientHost.PasswordRequested -= ClientHost_PasswordRequested;
ClientHost.ReconfigurationAborted -= ClientHost_ReconfigurationAborted;
ClientHost.ReconfigurationDenied -= ClientHost_ReconfigurationDenied;
ClientHost.ServerFailureActionRequested -= ClientHost_ServerFailureActionRequested;
ClientHost.Shutdown -= ClientHost_Shutdown;
}
runtime.ConnectionLost -= Runtime_ConnectionLost;
}
private void RegisterEvents()
{
ClientHost.ExamSelectionRequested += ClientHost_ExamSelectionRequested;
ClientHost.MessageBoxRequested += ClientHost_MessageBoxRequested;
ClientHost.PasswordRequested += ClientHost_PasswordRequested;
ClientHost.ReconfigurationAborted += ClientHost_ReconfigurationAborted;
ClientHost.ReconfigurationDenied += ClientHost_ReconfigurationDenied;
ClientHost.ServerFailureActionRequested += ClientHost_ServerFailureActionRequested;
ClientHost.Shutdown += ClientHost_Shutdown;
runtime.ConnectionLost += Runtime_ConnectionLost;
}
private void ClientHost_ExamSelectionRequested(ExamSelectionRequestEventArgs args)
{
Logger.Info($"Received exam selection request with id '{args.RequestId}'.");
var exams = args.Exams.Select(e => new Exam { Id = e.id, LmsName = e.lms, Name = e.name, Url = e.url });
var dialog = uiFactory.CreateExamSelectionDialog(exams);
var result = dialog.Show();
runtime.SubmitExamSelectionResult(args.RequestId, result.Success, result.SelectedExam?.Id);
Logger.Info($"Exam selection request with id '{args.RequestId}' is complete.");
}
private void ClientHost_MessageBoxRequested(MessageBoxRequestEventArgs args)
{
Logger.Info($"Received message box request with id '{args.RequestId}'.");
var action = (MessageBoxAction) args.Action;
var icon = (MessageBoxIcon) args.Icon;
var result = messageBox.Show(args.Message, args.Title, action, icon, parent: splashScreen);
runtime.SubmitMessageBoxResult(args.RequestId, (int) result);
Logger.Info($"Message box request with id '{args.RequestId}' yielded result '{result}'.");
}
private void ClientHost_PasswordRequested(PasswordRequestEventArgs args)
{
var message = default(TextKey);
var title = default(TextKey);
Logger.Info($"Received input request with id '{args.RequestId}' for the {args.Purpose.ToString().ToLower()} password.");
switch (args.Purpose)
{
case PasswordRequestPurpose.LocalAdministrator:
message = TextKey.PasswordDialog_LocalAdminPasswordRequired;
title = TextKey.PasswordDialog_LocalAdminPasswordRequiredTitle;
break;
case PasswordRequestPurpose.LocalSettings:
message = TextKey.PasswordDialog_LocalSettingsPasswordRequired;
title = TextKey.PasswordDialog_LocalSettingsPasswordRequiredTitle;
break;
case PasswordRequestPurpose.Settings:
message = TextKey.PasswordDialog_SettingsPasswordRequired;
title = TextKey.PasswordDialog_SettingsPasswordRequiredTitle;
break;
}
var dialog = uiFactory.CreatePasswordDialog(text.Get(message), text.Get(title));
var result = dialog.Show();
runtime.SubmitPassword(args.RequestId, result.Success, result.Password);
Logger.Info($"Password request with id '{args.RequestId}' was {(result.Success ? "successful" : "aborted by the user")}.");
}
private void ClientHost_ReconfigurationAborted()
{
Logger.Info("The reconfiguration was aborted by the runtime.");
splashScreen.Hide();
coordinator.ReleaseReconfigurationLock();
}
private void ClientHost_ReconfigurationDenied(ReconfigurationEventArgs args)
{
Logger.Info($"The reconfiguration request for '{args.ConfigurationPath}' was denied by the runtime!");
messageBox.Show(TextKey.MessageBox_ReconfigurationDenied, TextKey.MessageBox_ReconfigurationDeniedTitle, parent: splashScreen);
splashScreen.Hide();
coordinator.ReleaseReconfigurationLock();
}
private void ClientHost_ServerFailureActionRequested(ServerFailureActionRequestEventArgs args)
{
Logger.Info($"Received server failure action request with id '{args.RequestId}'.");
var dialog = uiFactory.CreateServerFailureDialog(args.Message, args.ShowFallback);
var result = dialog.Show();
runtime.SubmitServerFailureActionResult(args.RequestId, result.Abort, result.Fallback, result.Retry);
Logger.Info($"Server failure action request with id '{args.RequestId}' is complete, the user chose to {(result.Abort ? "abort" : (result.Fallback ? "fallback" : "retry"))}.");
}
private void ClientHost_Shutdown()
{
shutdown.Invoke();
}
private void Runtime_ConnectionLost()
{
Logger.Error("Lost connection to the runtime!");
messageBox.Show(TextKey.MessageBox_ApplicationError, TextKey.MessageBox_ApplicationErrorTitle, icon: MessageBoxIcon.Error);
shutdown.Invoke();
}
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Linq;
using System.Threading.Tasks;
using SafeExamBrowser.Configuration.Contracts.Integrity;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Windows.Data;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class IntegrityResponsibility : ClientResponsibility
{
private readonly IText text;
private IIntegrityModule IntegrityModule => Context.IntegrityModule;
public IntegrityResponsibility(ClientContext context, ILogger logger, IText text) : base(context, logger)
{
this.text = text;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.ScheduleIntegrityVerification:
ScheduleIntegrityVerification();
break;
case ClientTask.UpdateSessionIntegrity:
UpdateSessionIntegrity();
break;
case ClientTask.VerifySessionIntegrity:
VerifySessionIntegrity();
break;
}
}
private void ScheduleIntegrityVerification()
{
const int FIVE_MINUTES = 300000;
const int TEN_MINUTES = 600000;
var timer = new System.Timers.Timer();
timer.AutoReset = false;
timer.Elapsed += (o, args) => VerifyApplicationIntegrity();
timer.Interval = TEN_MINUTES + (new Random().NextDouble() * FIVE_MINUTES);
timer.Start();
}
private void UpdateSessionIntegrity()
{
var hasQuitPassword = !string.IsNullOrEmpty(Settings?.Security.QuitPasswordHash);
if (hasQuitPassword)
{
IntegrityModule?.ClearSession(Settings.Browser.ConfigurationKey, Settings.Browser.StartUrl);
}
}
private void VerifyApplicationIntegrity()
{
Logger.Info($"Attempting to verify application integrity...");
if (IntegrityModule.TryVerifyCodeSignature(out var isValid))
{
if (isValid)
{
Logger.Info("Application integrity successfully verified.");
}
else
{
Logger.Warn("Application integrity is compromised!");
ShowLockScreen(text.Get(TextKey.LockScreen_ApplicationIntegrityMessage), text.Get(TextKey.LockScreen_Title), Enumerable.Empty<LockScreenOption>());
}
}
else
{
Logger.Warn("Failed to verify application integrity!");
}
}
private void VerifySessionIntegrity()
{
var hasQuitPassword = !string.IsNullOrEmpty(Settings.Security.QuitPasswordHash);
if (hasQuitPassword && Settings.Security.VerifySessionIntegrity)
{
Logger.Info($"Attempting to verify session integrity...");
if (IntegrityModule.TryVerifySessionIntegrity(Settings.Browser.ConfigurationKey, Settings.Browser.StartUrl, out var isValid))
{
if (isValid)
{
Logger.Info("Session integrity successfully verified.");
IntegrityModule.CacheSession(Settings.Browser.ConfigurationKey, Settings.Browser.StartUrl);
}
else
{
Logger.Warn("Session integrity is compromised!");
Task.Delay(1000).ContinueWith(_ =>
{
ShowLockScreen(text.Get(TextKey.LockScreen_SessionIntegrityMessage), text.Get(TextKey.LockScreen_Title), Enumerable.Empty<LockScreenOption>());
});
}
}
else
{
Logger.Warn("Failed to verify session integrity!");
}
}
}
}
}

View File

@@ -0,0 +1,327 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using SafeExamBrowser.Client.Contracts;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Monitoring.Contracts.Applications;
using SafeExamBrowser.Monitoring.Contracts.Display;
using SafeExamBrowser.Monitoring.Contracts.System;
using SafeExamBrowser.Monitoring.Contracts.System.Events;
using SafeExamBrowser.UserInterface.Contracts.Shell;
using SafeExamBrowser.UserInterface.Contracts.Windows.Data;
using SafeExamBrowser.WindowsApi.Contracts;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class MonitoringResponsibility : ClientResponsibility
{
private readonly IActionCenter actionCenter;
private readonly IApplicationMonitor applicationMonitor;
private readonly ICoordinator coordinator;
private readonly IDisplayMonitor displayMonitor;
private readonly IExplorerShell explorerShell;
private readonly ISystemSentinel sentinel;
private readonly ITaskbar taskbar;
private readonly IText text;
public MonitoringResponsibility(
IActionCenter actionCenter,
IApplicationMonitor applicationMonitor,
ClientContext context,
ICoordinator coordinator,
IDisplayMonitor displayMonitor,
IExplorerShell explorerShell,
ILogger logger,
ISystemSentinel sentinel,
ITaskbar taskbar,
IText text) : base(context, logger)
{
this.actionCenter = actionCenter;
this.applicationMonitor = applicationMonitor;
this.coordinator = coordinator;
this.displayMonitor = displayMonitor;
this.explorerShell = explorerShell;
this.sentinel = sentinel;
this.taskbar = taskbar;
this.text = text;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.PrepareShutdown_Wave2:
StopMonitoring();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
case ClientTask.StartMonitoring:
StartMonitoring();
break;
}
}
private void DeregisterEvents()
{
applicationMonitor.ExplorerStarted -= ApplicationMonitor_ExplorerStarted;
applicationMonitor.TerminationFailed -= ApplicationMonitor_TerminationFailed;
displayMonitor.DisplayChanged -= DisplayMonitor_DisplaySettingsChanged;
sentinel.CursorChanged -= Sentinel_CursorChanged;
sentinel.EaseOfAccessChanged -= Sentinel_EaseOfAccessChanged;
sentinel.SessionChanged -= Sentinel_SessionChanged;
sentinel.StickyKeysChanged -= Sentinel_StickyKeysChanged;
}
private void StopMonitoring()
{
sentinel.StopMonitoring();
}
private void RegisterEvents()
{
applicationMonitor.ExplorerStarted += ApplicationMonitor_ExplorerStarted;
applicationMonitor.TerminationFailed += ApplicationMonitor_TerminationFailed;
displayMonitor.DisplayChanged += DisplayMonitor_DisplaySettingsChanged;
sentinel.CursorChanged += Sentinel_CursorChanged;
sentinel.EaseOfAccessChanged += Sentinel_EaseOfAccessChanged;
sentinel.SessionChanged += Sentinel_SessionChanged;
sentinel.StickyKeysChanged += Sentinel_StickyKeysChanged;
}
private void StartMonitoring()
{
sentinel.StartMonitoringSystemEvents();
if (!Settings.Security.AllowStickyKeys)
{
sentinel.StartMonitoringStickyKeys();
}
if (Settings.Security.VerifyCursorConfiguration)
{
sentinel.StartMonitoringCursors();
}
if (Settings.Service.IgnoreService)
{
sentinel.StartMonitoringEaseOfAccess();
}
}
private void ApplicationMonitor_ExplorerStarted()
{
Logger.Info("Trying to terminate Windows explorer...");
explorerShell.Terminate();
Logger.Info("Re-initializing working area...");
displayMonitor.InitializePrimaryDisplay(Settings.UserInterface.Taskbar.EnableTaskbar ? taskbar.GetAbsoluteHeight() : 0);
Logger.Info("Re-initializing shell...");
actionCenter.InitializeBounds();
taskbar.InitializeBounds();
Logger.Info("Desktop successfully restored.");
}
private void ApplicationMonitor_TerminationFailed(IEnumerable<RunningApplication> applications)
{
var applicationList = string.Join(Environment.NewLine, applications.Select(a => $"- {a.Name}"));
var message = $"{text.Get(TextKey.LockScreen_ApplicationsMessage)}{Environment.NewLine}{Environment.NewLine}{applicationList}";
var title = text.Get(TextKey.LockScreen_Title);
var allowOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_ApplicationsAllowOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_ApplicationsTerminateOption) };
Logger.Warn("Detected termination failure of blacklisted application(s)!");
var result = ShowLockScreen(message, title, new[] { allowOption, terminateOption });
if (result.OptionId == allowOption.Id)
{
Logger.Info($"The blacklisted application(s) {string.Join(", ", applications.Select(a => $"'{a.Name}'"))} will be temporarily allowed.");
}
else if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
}
private void DisplayMonitor_DisplaySettingsChanged()
{
Logger.Info("Re-initializing working area...");
displayMonitor.InitializePrimaryDisplay(Settings.UserInterface.Taskbar.EnableTaskbar ? taskbar.GetAbsoluteHeight() : 0);
Logger.Info("Re-initializing shell...");
actionCenter.InitializeBounds();
Context.LockScreen?.InitializeBounds();
taskbar.InitializeBounds();
Logger.Info("Desktop successfully restored.");
if (!displayMonitor.ValidateConfiguration(Settings.Display).IsAllowed)
{
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_DisplayConfigurationContinueOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_DisplayConfigurationTerminateOption) };
var message = text.Get(TextKey.LockScreen_DisplayConfigurationMessage);
var title = text.Get(TextKey.LockScreen_Title);
var result = ShowLockScreen(message, title, new[] { continueOption, terminateOption });
if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
}
}
private void Sentinel_CursorChanged(SentinelEventArgs args)
{
if (coordinator.RequestSessionLock())
{
var message = text.Get(TextKey.LockScreen_CursorMessage);
var title = text.Get(TextKey.LockScreen_Title);
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_CursorContinueOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_CursorTerminateOption) };
args.Allow = true;
Logger.Info("Cursor changed! Attempting to show lock screen...");
var result = ShowLockScreen(message, title, new[] { continueOption, terminateOption });
if (result.OptionId == continueOption.Id)
{
Logger.Info("The session will be allowed to resume as requested by the user...");
}
else if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
coordinator.ReleaseSessionLock();
}
else
{
Logger.Info("Cursor changed but lock screen is already active.");
}
}
private void Sentinel_EaseOfAccessChanged(SentinelEventArgs args)
{
if (coordinator.RequestSessionLock())
{
var message = text.Get(TextKey.LockScreen_EaseOfAccessMessage);
var title = text.Get(TextKey.LockScreen_Title);
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_EaseOfAccessContinueOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_EaseOfAccessTerminateOption) };
args.Allow = true;
Logger.Info("Ease of access changed! Attempting to show lock screen...");
var result = ShowLockScreen(message, title, new[] { continueOption, terminateOption });
if (result.OptionId == continueOption.Id)
{
Logger.Info("The session will be allowed to resume as requested by the user...");
}
else if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
coordinator.ReleaseSessionLock();
}
else
{
Logger.Info("Ease of access changed but lock screen is already active.");
}
}
private void Sentinel_SessionChanged()
{
var allow = !Settings.Service.IgnoreService && (!Settings.Service.DisableUserLock || !Settings.Service.DisableUserSwitch);
var disable = Settings.Security.DisableSessionChangeLockScreen;
if (allow || disable)
{
Logger.Info($"Detected user session change, but {(allow ? "session locking and/or switching is allowed" : "lock screen is deactivated")}.");
}
else
{
var message = text.Get(TextKey.LockScreen_UserSessionMessage);
var title = text.Get(TextKey.LockScreen_Title);
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionContinueOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionTerminateOption) };
Logger.Warn("User session changed! Attempting to show lock screen...");
if (coordinator.RequestSessionLock())
{
var result = ShowLockScreen(message, title, new[] { continueOption, terminateOption });
if (result.OptionId == continueOption.Id)
{
Logger.Info("The session will be allowed to resume as requested by the user...");
}
else if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
coordinator.ReleaseSessionLock();
}
else
{
Logger.Warn("User session changed but lock screen is already active.");
}
}
}
private void Sentinel_StickyKeysChanged(SentinelEventArgs args)
{
if (coordinator.RequestSessionLock())
{
var message = text.Get(TextKey.LockScreen_StickyKeysMessage);
var title = text.Get(TextKey.LockScreen_Title);
var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_StickyKeysContinueOption) };
var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_StickyKeysTerminateOption) };
args.Allow = true;
Logger.Info("Sticky keys changed! Attempting to show lock screen...");
var result = ShowLockScreen(message, title, new[] { continueOption, terminateOption });
if (result.OptionId == continueOption.Id)
{
Logger.Info("The session will be allowed to resume as requested by the user...");
}
else if (result.OptionId == terminateOption.Id)
{
Logger.Info("Attempting to shutdown as requested by the user...");
TryRequestShutdown();
}
coordinator.ReleaseSessionLock();
}
else
{
Logger.Info("Sticky keys changed but lock screen is already active.");
}
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.SystemComponents.Contracts.Network;
using SafeExamBrowser.SystemComponents.Contracts.Network.Events;
using SafeExamBrowser.UserInterface.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Windows.Data;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class NetworkResponsibility : ClientResponsibility
{
private readonly INetworkAdapter networkAdapter;
private readonly IText text;
private readonly IUserInterfaceFactory uiFactory;
public NetworkResponsibility(ClientContext context, ILogger logger, INetworkAdapter networkAdapter, IText text, IUserInterfaceFactory uiFactory) : base(context, logger)
{
this.networkAdapter = networkAdapter;
this.text = text;
this.uiFactory = uiFactory;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
}
}
private void DeregisterEvents()
{
networkAdapter.CredentialsRequired -= NetworkAdapter_CredentialsRequired;
}
private void RegisterEvents()
{
networkAdapter.CredentialsRequired += NetworkAdapter_CredentialsRequired;
}
private void NetworkAdapter_CredentialsRequired(CredentialsRequiredEventArgs args)
{
var message = text.Get(TextKey.CredentialsDialog_WirelessNetworkMessage).Replace("%%_NAME_%%", args.NetworkName);
var title = text.Get(TextKey.CredentialsDialog_WirelessNetworkTitle);
var dialog = uiFactory.CreateCredentialsDialog(CredentialsDialogPurpose.WirelessNetwork, message, title);
var result = dialog.Show();
args.Password = result.Password;
args.Success = result.Success;
args.Username = result.Username;
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System.Threading.Tasks;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Proctoring.Contracts;
using SafeExamBrowser.Proctoring.Contracts.Events;
using SafeExamBrowser.UserInterface.Contracts;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class ProctoringResponsibility : ClientResponsibility
{
private readonly IUserInterfaceFactory uiFactory;
private IProctoringController Proctoring => Context.Proctoring;
public ProctoringResponsibility(ClientContext context, ILogger logger, IUserInterfaceFactory uiFactory) : base(context, logger)
{
this.uiFactory = uiFactory;
}
public override void Assume(ClientTask task)
{
if (task == ClientTask.PrepareShutdown_Wave1)
{
FinalizeProctoring();
}
}
private void FinalizeProctoring()
{
if (Proctoring != default && Proctoring.HasRemainingWork())
{
var dialog = uiFactory.CreateProctoringFinalizationDialog();
var handler = new RemainingWorkUpdatedEventHandler((args) => dialog.Update(args));
Task.Run(() =>
{
Proctoring.RemainingWorkUpdated += handler;
Proctoring.ExecuteRemainingWork();
Proctoring.RemainingWorkUpdated -= handler;
});
dialog.Show();
}
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System.Linq;
using SafeExamBrowser.Client.Contracts;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.Server.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Windows.Data;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class ServerResponsibility : ClientResponsibility
{
private readonly ICoordinator coordinator;
private readonly IText text;
private IServerProxy Server => Context.Server;
public ServerResponsibility(ClientContext context, ICoordinator coordinator, ILogger logger, IText text) : base(context, logger)
{
this.coordinator = coordinator;
this.text = text;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
}
}
private void DeregisterEvents()
{
if (Server != default)
{
Server.LockScreenConfirmed -= Server_LockScreenConfirmed;
Server.LockScreenRequested -= Server_LockScreenRequested;
Server.TerminationRequested -= Server_TerminationRequested;
}
}
private void RegisterEvents()
{
Server.LockScreenConfirmed += Server_LockScreenConfirmed;
Server.LockScreenRequested += Server_LockScreenRequested;
Server.TerminationRequested += Server_TerminationRequested;
}
private void Server_LockScreenConfirmed()
{
Logger.Info("Closing lock screen as requested by the server...");
Context.LockScreen?.Cancel();
}
private void Server_LockScreenRequested(string message)
{
Logger.Info("Attempting to show lock screen as requested by the server...");
if (coordinator.RequestSessionLock())
{
ShowLockScreen(message, text.Get(TextKey.LockScreen_Title), Enumerable.Empty<LockScreenOption>());
coordinator.ReleaseSessionLock();
}
else
{
Logger.Info("Lock screen is already active.");
}
}
private void Server_TerminationRequested()
{
Logger.Info("Attempting to shutdown as requested by the server...");
TryRequestShutdown();
}
}
}

View File

@@ -0,0 +1,186 @@
/*
* Copyright (c) 2025 ETH Zürich, IT Services
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using System.ComponentModel;
using System.Linq;
using SafeExamBrowser.Configuration.Contracts.Cryptography;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.Logging.Contracts;
using SafeExamBrowser.UserInterface.Contracts;
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
using SafeExamBrowser.UserInterface.Contracts.Shell;
namespace SafeExamBrowser.Client.Responsibilities
{
internal class ShellResponsibility : ClientResponsibility
{
private readonly IActionCenter actionCenter;
private readonly IHashAlgorithm hashAlgorithm;
private readonly IMessageBox messageBox;
private readonly ITaskbar taskbar;
private readonly IUserInterfaceFactory uiFactory;
public ShellResponsibility(
IActionCenter actionCenter,
ClientContext context,
IHashAlgorithm hashAlgorithm,
ILogger logger,
IMessageBox messageBox,
ITaskbar taskbar,
IUserInterfaceFactory uiFactory) : base(context, logger)
{
this.actionCenter = actionCenter;
this.hashAlgorithm = hashAlgorithm;
this.messageBox = messageBox;
this.taskbar = taskbar;
this.uiFactory = uiFactory;
}
public override void Assume(ClientTask task)
{
switch (task)
{
case ClientTask.CloseShell:
CloseShell();
break;
case ClientTask.DeregisterEvents:
DeregisterEvents();
break;
case ClientTask.RegisterEvents:
RegisterEvents();
break;
case ClientTask.ShowShell:
ShowShell();
break;
}
}
private void DeregisterEvents()
{
actionCenter.QuitButtonClicked -= Shell_QuitButtonClicked;
taskbar.LoseFocusRequested -= Taskbar_LoseFocusRequested;
taskbar.QuitButtonClicked -= Shell_QuitButtonClicked;
foreach (var activator in Context.Activators.OfType<ITerminationActivator>())
{
activator.Activated -= TerminationActivator_Activated;
}
}
private void RegisterEvents()
{
actionCenter.QuitButtonClicked += Shell_QuitButtonClicked;
taskbar.LoseFocusRequested += Taskbar_LoseFocusRequested;
taskbar.QuitButtonClicked += Shell_QuitButtonClicked;
foreach (var activator in Context.Activators.OfType<ITerminationActivator>())
{
activator.Activated += TerminationActivator_Activated;
}
}
private void CloseShell()
{
if (Settings?.UserInterface.ActionCenter.EnableActionCenter == true)
{
actionCenter.Close();
}
if (Settings?.UserInterface.Taskbar.EnableTaskbar == true)
{
taskbar.Close();
}
}
private void ShowShell()
{
if (Settings.UserInterface.ActionCenter.EnableActionCenter)
{
actionCenter.Promote();
}
if (Settings.UserInterface.Taskbar.EnableTaskbar)
{
taskbar.Show();
}
}
private void Shell_QuitButtonClicked(CancelEventArgs args)
{
PauseActivators();
args.Cancel = !TryInitiateShutdown();
ResumeActivators();
}
private void Taskbar_LoseFocusRequested(bool forward)
{
Context.Browser.Focus(forward);
}
private void TerminationActivator_Activated()
{
PauseActivators();
TryInitiateShutdown();
ResumeActivators();
}
private bool TryInitiateShutdown()
{
var hasQuitPassword = !string.IsNullOrEmpty(Settings.Security.QuitPasswordHash);
var initiateShutdown = hasQuitPassword ? TryValidateQuitPassword() : TryConfirmShutdown();
var succes = false;
if (initiateShutdown)
{
succes = TryRequestShutdown();
}
return succes;
}
private bool TryConfirmShutdown()
{
var result = messageBox.Show(TextKey.MessageBox_Quit, TextKey.MessageBox_QuitTitle, MessageBoxAction.YesNo, MessageBoxIcon.Question);
var quit = result == MessageBoxResult.Yes;
if (quit)
{
Logger.Info("The user chose to terminate the application.");
}
return quit;
}
private bool TryValidateQuitPassword()
{
var dialog = uiFactory.CreatePasswordDialog(TextKey.PasswordDialog_QuitPasswordRequired, TextKey.PasswordDialog_QuitPasswordRequiredTitle);
var result = dialog.Show();
if (result.Success)
{
var passwordHash = hashAlgorithm.GenerateHashFor(result.Password);
var isCorrect = Settings.Security.QuitPasswordHash.Equals(passwordHash, StringComparison.OrdinalIgnoreCase);
if (isCorrect)
{
Logger.Info("The user entered the correct quit password, the application will now terminate.");
}
else
{
Logger.Info("The user entered the wrong quit password.");
messageBox.Show(TextKey.MessageBox_InvalidQuitPassword, TextKey.MessageBox_InvalidQuitPasswordTitle, icon: MessageBoxIcon.Warning);
}
return isCorrect;
}
return false;
}
}
}