2018-02-06 15:12:11 +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;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using System.IO;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using System.Linq;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
using SafeExamBrowser.Configuration.DataFormats;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2018-08-31 07:49:41 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Configuration
|
|
|
|
|
{
|
|
|
|
|
public class ConfigurationRepository : IConfigurationRepository
|
|
|
|
|
{
|
2018-03-14 11:04:28 +01:00
|
|
|
|
private const string BASE_ADDRESS = "net.pipe://localhost/safeexambrowser";
|
|
|
|
|
|
2018-09-04 10:58:56 +02:00
|
|
|
|
private readonly string executablePath;
|
|
|
|
|
private readonly string programCopyright;
|
|
|
|
|
private readonly string programTitle;
|
|
|
|
|
private readonly string programVersion;
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private AppConfig appConfig;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private IHashAlgorithm hashAlgorithm;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
private IList<IDataFormat> dataFormats;
|
|
|
|
|
private IList<IResourceLoader> resourceLoaders;
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private ILogger logger;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
public ConfigurationRepository(
|
|
|
|
|
IHashAlgorithm hashAlgorithm,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
string executablePath,
|
|
|
|
|
string programCopyright,
|
|
|
|
|
string programTitle,
|
|
|
|
|
string programVersion)
|
2018-09-04 10:58:56 +02:00
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
dataFormats = new List<IDataFormat>();
|
|
|
|
|
resourceLoaders = new List<IResourceLoader>();
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
this.hashAlgorithm = hashAlgorithm;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
this.logger = logger;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
this.executablePath = executablePath ?? string.Empty;
|
|
|
|
|
this.programCopyright = programCopyright ?? string.Empty;
|
|
|
|
|
this.programTitle = programTitle ?? string.Empty;
|
|
|
|
|
this.programVersion = programVersion ?? string.Empty;
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 11:16:59 +02:00
|
|
|
|
public AppConfig InitializeAppConfig()
|
2018-02-06 15:12:11 +01:00
|
|
|
|
{
|
|
|
|
|
var appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(SafeExamBrowser));
|
|
|
|
|
var startTime = DateTime.Now;
|
|
|
|
|
var logFolder = Path.Combine(appDataFolder, "Logs");
|
|
|
|
|
var logFilePrefix = startTime.ToString("yyyy-MM-dd\\_HH\\hmm\\mss\\s");
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig = new AppConfig();
|
|
|
|
|
appConfig.ApplicationStartTime = startTime;
|
|
|
|
|
appConfig.AppDataFolder = appDataFolder;
|
|
|
|
|
appConfig.BrowserCachePath = Path.Combine(appDataFolder, "Cache");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.BrowserLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Browser.log");
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ClientId = Guid.NewGuid();
|
|
|
|
|
appConfig.ClientAddress = $"{BASE_ADDRESS}/client/{Guid.NewGuid()}";
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ClientExecutablePath = Path.Combine(Path.GetDirectoryName(executablePath), $"{nameof(SafeExamBrowser)}.Client.exe");
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.ClientLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Client.log");
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ConfigurationFileExtension = ".seb";
|
|
|
|
|
appConfig.DefaultSettingsFileName = "SebClientSettings.seb";
|
|
|
|
|
appConfig.DownloadDirectory = Path.Combine(appDataFolder, "Downloads");
|
2018-08-31 07:49:41 +02:00
|
|
|
|
appConfig.LogLevel = LogLevel.Debug;
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ProgramCopyright = programCopyright;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ProgramDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), nameof(SafeExamBrowser));
|
2018-09-04 10:58:56 +02:00
|
|
|
|
appConfig.ProgramTitle = programTitle;
|
|
|
|
|
appConfig.ProgramVersion = programVersion;
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.RuntimeId = Guid.NewGuid();
|
|
|
|
|
appConfig.RuntimeAddress = $"{BASE_ADDRESS}/runtime/{Guid.NewGuid()}";
|
2018-08-16 11:23:37 +02:00
|
|
|
|
appConfig.RuntimeLogFile = Path.Combine(logFolder, $"{logFilePrefix}_Runtime.log");
|
2018-08-14 09:06:35 +02:00
|
|
|
|
appConfig.SebUriScheme = "seb";
|
|
|
|
|
appConfig.SebUriSchemeSecure = "sebs";
|
2018-06-29 09:50:20 +02:00
|
|
|
|
appConfig.ServiceAddress = $"{BASE_ADDRESS}/service";
|
2018-10-12 11:16:59 +02:00
|
|
|
|
|
|
|
|
|
return appConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISessionConfiguration InitializeSessionConfiguration()
|
|
|
|
|
{
|
|
|
|
|
var configuration = new SessionConfiguration();
|
|
|
|
|
|
|
|
|
|
UpdateAppConfig();
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
configuration.AppConfig = appConfig.Clone();
|
2018-10-12 11:16:59 +02:00
|
|
|
|
configuration.Id = Guid.NewGuid();
|
|
|
|
|
configuration.StartupToken = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Settings LoadDefaultSettings()
|
|
|
|
|
{
|
|
|
|
|
var settings = new Settings();
|
|
|
|
|
|
2018-11-22 14:36:20 +01:00
|
|
|
|
// TODO: Specify default settings
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.KioskMode = KioskMode.None;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.ServicePolicy = ServicePolicy.Optional;
|
|
|
|
|
|
|
|
|
|
settings.Browser.StartUrl = "https://www.safeexambrowser.org/testing";
|
|
|
|
|
settings.Browser.AllowAddressBar = true;
|
|
|
|
|
settings.Browser.AllowBackwardNavigation = true;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.Browser.AllowConfigurationDownloads = true;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.Browser.AllowDeveloperConsole = true;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
settings.Browser.AllowDownloads = true;
|
2018-10-12 11:16:59 +02:00
|
|
|
|
settings.Browser.AllowForwardNavigation = true;
|
|
|
|
|
settings.Browser.AllowReloading = true;
|
|
|
|
|
|
|
|
|
|
settings.Taskbar.AllowApplicationLog = true;
|
|
|
|
|
settings.Taskbar.AllowKeyboardLayout = true;
|
|
|
|
|
settings.Taskbar.AllowWirelessNetwork = true;
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
public void Register(IDataFormat dataFormat)
|
|
|
|
|
{
|
|
|
|
|
dataFormats.Add(dataFormat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Register(IResourceLoader resourceLoader)
|
|
|
|
|
{
|
|
|
|
|
resourceLoaders.Add(resourceLoader);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
public LoadStatus TryLoadSettings(Uri resource, PasswordInfo passwordInfo, out Settings settings)
|
2018-10-12 11:16:59 +02:00
|
|
|
|
{
|
2018-11-08 09:39:52 +01:00
|
|
|
|
logger.Info($"Attempting to load '{resource}'...");
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
settings = LoadDefaultSettings();
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-11-09 14:15:56 +01:00
|
|
|
|
var status = TryLoadData(resource, out Stream data);
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
using (data)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
if (status == LoadStatus.LoadWithBrowser)
|
|
|
|
|
{
|
|
|
|
|
return HandleBrowserResource(resource, settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status != LoadStatus.Success)
|
2018-11-15 08:45:17 +01:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return status;
|
2018-11-15 08:45:17 +01:00
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return TryParseData(data, passwordInfo, resource, settings);
|
2018-11-30 14:50:28 +01:00
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Unexpected error while trying to load '{resource}'!", e);
|
|
|
|
|
|
|
|
|
|
return LoadStatus.UnexpectedError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private void ExtractAndImportCertificates(IDictionary<string, object> data)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LoadStatus HandleBrowserResource(Uri resource, Settings settings)
|
|
|
|
|
{
|
|
|
|
|
settings.Browser.StartUrl = resource.AbsoluteUri;
|
|
|
|
|
logger.Info($"The resource needs authentication or is HTML data, loaded default settings with '{resource}' as startup URL.");
|
|
|
|
|
|
|
|
|
|
return LoadStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleParseSuccess(ParseResult result, Settings settings, PasswordInfo passwordInfo, Uri resource)
|
|
|
|
|
{
|
|
|
|
|
var appDataFile = new Uri(Path.Combine(appConfig.AppDataFolder, appConfig.DefaultSettingsFileName));
|
|
|
|
|
var programDataFile = new Uri(Path.Combine(appConfig.ProgramDataFolder, appConfig.DefaultSettingsFileName));
|
|
|
|
|
var isAppDataFile = resource.AbsolutePath.Equals(appDataFile.AbsolutePath, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
var isProgramDataFile = resource.AbsolutePath.Equals(programDataFile.AbsolutePath, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
logger.Info("Mapping settings data...");
|
|
|
|
|
result.RawData.MapTo(settings);
|
|
|
|
|
|
|
|
|
|
if (settings.ConfigurationMode == ConfigurationMode.ConfigureClient && !isAppDataFile && !isProgramDataFile)
|
|
|
|
|
{
|
|
|
|
|
result.Status = TryConfigureClient(result.RawData, settings, passwordInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 14:15:56 +01:00
|
|
|
|
private LoadStatus TryLoadData(Uri resource, out Stream data)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-09 14:15:56 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
|
|
|
|
var resourceLoader = resourceLoaders.FirstOrDefault(l => l.CanLoad(resource));
|
|
|
|
|
|
|
|
|
|
data = default(Stream);
|
|
|
|
|
|
|
|
|
|
if (resourceLoader != null)
|
|
|
|
|
{
|
|
|
|
|
status = resourceLoader.TryLoad(resource, out data);
|
|
|
|
|
logger.Info($"Tried to load data from '{resource}' using {resourceLoader.GetType().Name} -> Result: {status}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No resource loader found for '{resource}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-11-08 09:39:52 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private LoadStatus TryParseData(Stream data, PasswordInfo passwordInfo, Uri resource, Settings settings)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
|
|
|
|
var dataFormat = dataFormats.FirstOrDefault(f => f.CanParse(data));
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var status = LoadStatus.NotSupported;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
|
|
|
|
if (dataFormat != null)
|
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
var result = dataFormat.TryParse(data, passwordInfo);
|
|
|
|
|
|
|
|
|
|
logger.Info($"Tried to parse data from '{data}' using {dataFormat.GetType().Name} -> Result: {result.Status}.");
|
|
|
|
|
|
|
|
|
|
if (result.Status == LoadStatus.Success || result.Status == LoadStatus.SuccessConfigureClient)
|
|
|
|
|
{
|
|
|
|
|
HandleParseSuccess(result, settings, passwordInfo, resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status = result.Status;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn($"No data format found for '{data}'!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
private LoadStatus TryConfigureClient(IDictionary<string, object> data, Settings settings, PasswordInfo passwordInfo)
|
2018-11-09 14:15:56 +01:00
|
|
|
|
{
|
2018-12-11 16:06:10 +01:00
|
|
|
|
logger.Info("Attempting to configure local client settings...");
|
2018-11-09 14:15:56 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
if (passwordInfo.AdminPasswordHash != null)
|
|
|
|
|
{
|
|
|
|
|
var adminPasswordHash = passwordInfo.AdminPassword != null ? hashAlgorithm.GenerateHashFor(passwordInfo.AdminPassword) : null;
|
|
|
|
|
var settingsPasswordHash = passwordInfo.SettingsPassword != null ? hashAlgorithm.GenerateHashFor(passwordInfo.SettingsPassword) : null;
|
|
|
|
|
var enteredCorrectPassword = passwordInfo.AdminPasswordHash.Equals(adminPasswordHash, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
var sameAdminPassword = passwordInfo.AdminPasswordHash.Equals(settings.AdminPasswordHash, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
var knowsAdminPassword = passwordInfo.AdminPasswordHash.Equals(settingsPasswordHash, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
if (sameAdminPassword || knowsAdminPassword || enteredCorrectPassword)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Authentication was successful.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Authentication has failed!");
|
|
|
|
|
|
|
|
|
|
return LoadStatus.AdminPasswordNeeded;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Authentication is not required.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -> Certificates need to be imported and REMOVED from the settings before the data is saved!
|
|
|
|
|
ExtractAndImportCertificates(data);
|
|
|
|
|
|
|
|
|
|
// Save configuration data as local client config under %APPDATA%!
|
|
|
|
|
// -> Default settings password for local client configuration appears to be string.Empty
|
|
|
|
|
// -> Local client configuration needs to again be encrypted in the same way as the original file was!!
|
|
|
|
|
|
|
|
|
|
return LoadStatus.SuccessConfigureClient;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
private void UpdateAppConfig()
|
2018-03-14 11:04:28 +01:00
|
|
|
|
{
|
2018-10-12 11:16:59 +02:00
|
|
|
|
appConfig.ClientId = Guid.NewGuid();
|
|
|
|
|
appConfig.ClientAddress = $"{BASE_ADDRESS}/client/{Guid.NewGuid()}";
|
2018-03-14 11:04:28 +01:00
|
|
|
|
}
|
2018-02-06 15:12:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|