2018-01-18 15:14:05 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* 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;
|
2018-06-27 14:02:16 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Data;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2018-10-03 14:35:27 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel.Events;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-10-03 14:35:27 +02:00
|
|
|
|
using SafeExamBrowser.Runtime.Operations.Events;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Runtime.Operations
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
internal class ConfigurationOperation : SessionOperation
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
private string[] commandLineArgs;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private IConfigurationRepository configuration;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
private ILogger logger;
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override event ActionRequiredEventHandler ActionRequired;
|
|
|
|
|
public override event StatusChangedEventHandler StatusChanged;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
|
|
|
|
public ConfigurationOperation(
|
2018-10-12 11:16:59 +02:00
|
|
|
|
string[] commandLineArgs,
|
2018-06-29 09:50:20 +02:00
|
|
|
|
IConfigurationRepository configuration,
|
2018-01-18 15:14:05 +01:00
|
|
|
|
ILogger logger,
|
2018-10-12 11:16:59 +02:00
|
|
|
|
SessionContext sessionContext) : base(sessionContext)
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
this.commandLineArgs = commandLineArgs;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
this.configuration = configuration;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
this.logger = logger;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Perform()
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Initializing application configuration...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_InitializeConfiguration);
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
var result = OperationResult.Failed;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
var isValidUri = TryInitializeSettingsUri(out Uri uri);
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
|
|
|
|
if (isValidUri)
|
|
|
|
|
{
|
2018-11-15 08:45:17 +01:00
|
|
|
|
result = LoadSettings(uri);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = LoadDefaultSettings();
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
LogOperationResult(result);
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
return result;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Repeat()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-06-21 07:56:25 +02:00
|
|
|
|
logger.Info("Initializing new application configuration...");
|
2018-10-03 15:42:50 +02:00
|
|
|
|
StatusChanged?.Invoke(TextKey.OperationStatus_InitializeConfiguration);
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
var result = OperationResult.Failed;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
var isValidUri = TryValidateSettingsUri(Context.ReconfigurationFilePath, out Uri uri);
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
|
|
|
|
if (isValidUri)
|
|
|
|
|
{
|
2018-11-15 08:45:17 +01:00
|
|
|
|
result = LoadSettings(uri);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"The resource specified for reconfiguration does not exist or is not valid!");
|
2018-06-21 07:56:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
LogOperationResult(result);
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
return result;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public override OperationResult Revert()
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
2018-10-10 09:19:03 +02:00
|
|
|
|
return OperationResult.Success;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
private OperationResult LoadDefaultSettings()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("No valid configuration resource specified nor found in PROGRAMDATA or APPDATA - loading default settings...");
|
|
|
|
|
Context.Next.Settings = configuration.LoadDefaultSettings();
|
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private OperationResult LoadSettings(Uri uri)
|
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var settings = default(Settings);
|
|
|
|
|
var status = default(LoadStatus);
|
|
|
|
|
var passwordInfo = new PasswordInfo { AdminPasswordHash = Context.Current?.Settings?.AdminPasswordHash };
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
for (int adminAttempts = 0, settingsAttempts = 0; adminAttempts < 5 && settingsAttempts < 5;)
|
2018-06-21 07:56:25 +02:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
status = configuration.TryLoadSettings(uri, passwordInfo, out settings);
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
if (status != LoadStatus.AdminPasswordNeeded && status != LoadStatus.SettingsPasswordNeeded)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var success = TryGetPassword(status, passwordInfo);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
2018-06-21 07:56:25 +02:00
|
|
|
|
{
|
2018-11-22 14:36:20 +01:00
|
|
|
|
return OperationResult.Aborted;
|
2018-06-21 11:07:46 +02:00
|
|
|
|
}
|
2018-11-22 14:36:20 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
adminAttempts += status == LoadStatus.AdminPasswordNeeded ? 1 : 0;
|
|
|
|
|
settingsAttempts += status == LoadStatus.SettingsPasswordNeeded ? 1 : 0;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
Context.Next.Settings = settings;
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
if (status == LoadStatus.Success)
|
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return OperationResult.Success;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
|
|
|
|
if (status == LoadStatus.SuccessConfigureClient)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return HandleClientConfiguration();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
ShowFailureMessage(status, uri);
|
|
|
|
|
|
|
|
|
|
return OperationResult.Failed;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowFailureMessage(LoadStatus status, Uri uri)
|
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
switch (status)
|
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
case LoadStatus.AdminPasswordNeeded:
|
|
|
|
|
case LoadStatus.SettingsPasswordNeeded:
|
|
|
|
|
ActionRequired?.Invoke(new InvalidPasswordMessageArgs());
|
|
|
|
|
break;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
case LoadStatus.InvalidData:
|
|
|
|
|
ActionRequired?.Invoke(new InvalidDataMessageArgs(uri.ToString()));
|
|
|
|
|
break;
|
|
|
|
|
case LoadStatus.NotSupported:
|
|
|
|
|
ActionRequired?.Invoke(new NotSupportedMessageArgs(uri.ToString()));
|
|
|
|
|
break;
|
|
|
|
|
case LoadStatus.UnexpectedError:
|
|
|
|
|
ActionRequired?.Invoke(new UnexpectedErrorMessageArgs(uri.ToString()));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-06-21 07:56:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private bool TryGetPassword(LoadStatus status, PasswordInfo passwordInfo)
|
2018-06-21 07:56:25 +02:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var purpose = status == LoadStatus.AdminPasswordNeeded ? PasswordRequestPurpose.Administrator : PasswordRequestPurpose.Settings;
|
2018-10-03 14:35:27 +02:00
|
|
|
|
var args = new PasswordRequiredEventArgs { Purpose = purpose };
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
ActionRequired?.Invoke(args);
|
2018-08-10 13:23:24 +02:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
if (purpose == PasswordRequestPurpose.Administrator)
|
|
|
|
|
{
|
|
|
|
|
passwordInfo.AdminPassword = args.Password;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
passwordInfo.SettingsPassword = args.Password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args.Success;
|
2018-06-27 14:02:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private bool TryInitializeSettingsUri(out Uri uri)
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
|
|
|
|
var path = string.Empty;
|
|
|
|
|
var isValidUri = false;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
var programDataSettings = Path.Combine(Context.Next.AppConfig.ProgramDataFolder, Context.Next.AppConfig.DefaultSettingsFileName);
|
|
|
|
|
var appDataSettings = Path.Combine(Context.Next.AppConfig.AppDataFolder, Context.Next.AppConfig.DefaultSettingsFileName);
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
|
|
|
|
uri = null;
|
|
|
|
|
|
|
|
|
|
if (commandLineArgs?.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
path = commandLineArgs[1];
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
2018-11-09 14:15:56 +01:00
|
|
|
|
logger.Info($"Found command-line argument for configuration resource: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidUri && File.Exists(programDataSettings))
|
|
|
|
|
{
|
|
|
|
|
path = programDataSettings;
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
2018-11-09 14:15:56 +01:00
|
|
|
|
logger.Info($"Found configuration file in PROGRAMDATA: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidUri && File.Exists(appDataSettings))
|
|
|
|
|
{
|
|
|
|
|
path = appDataSettings;
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
2018-11-09 14:15:56 +01:00
|
|
|
|
logger.Info($"Found configuration file in APPDATA: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isValidUri;
|
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private bool TryValidateSettingsUri(string path, out Uri uri)
|
2018-03-21 10:23:15 +01:00
|
|
|
|
{
|
2018-06-21 07:56:25 +02:00
|
|
|
|
var isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
2018-03-21 10:23:15 +01:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
isValidUri &= uri != null && uri.IsFile;
|
|
|
|
|
isValidUri &= File.Exists(path);
|
2018-03-21 10:23:15 +01:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
return isValidUri;
|
2018-03-21 10:23:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private OperationResult HandleClientConfiguration()
|
2018-06-27 14:02:16 +02:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var firstSession = Context.Current == null;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
if (firstSession)
|
2018-06-27 14:02:16 +02:00
|
|
|
|
{
|
2018-10-03 14:35:27 +02:00
|
|
|
|
var args = new ConfigurationCompletedEventArgs();
|
2018-12-11 16:06:10 +01:00
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
ActionRequired?.Invoke(args);
|
|
|
|
|
logger.Info($"The user chose to {(args.AbortStartup ? "abort" : "continue")} after successful client configuration.");
|
2018-06-27 14:02:16 +02:00
|
|
|
|
|
2018-10-03 14:35:27 +02:00
|
|
|
|
if (args.AbortStartup)
|
2018-06-27 14:02:16 +02:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return OperationResult.Aborted;
|
2018-06-27 14:02:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-11 16:06:10 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// TODO: If the client configuration happens while the application is already running, the new configuration should first
|
|
|
|
|
// be loaded and then the user should have the option to terminate!
|
|
|
|
|
// -> Introduce flag in Context, e.g. AskForTermination?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-06-27 14:02:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
private void LogOperationResult(OperationResult result)
|
|
|
|
|
{
|
|
|
|
|
switch (result)
|
|
|
|
|
{
|
|
|
|
|
case OperationResult.Aborted:
|
|
|
|
|
logger.Info("The configuration was aborted by the user.");
|
|
|
|
|
break;
|
|
|
|
|
case OperationResult.Failed:
|
|
|
|
|
logger.Warn("The configuration has failed!");
|
|
|
|
|
break;
|
|
|
|
|
case OperationResult.Success:
|
|
|
|
|
logger.Info("The configuration was successful.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|