2023-03-27 10:24:40 +02:00
|
|
|
|
using Microsoft.VisualBasic.FileIO;
|
|
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
namespace Common;
|
|
|
|
|
|
|
2023-04-02 22:40:47 +02:00
|
|
|
|
public delegate void Error(Exception e);
|
2023-04-02 22:18:51 +02:00
|
|
|
|
public static class ConfigFileManager
|
2023-03-27 10:24:40 +02:00
|
|
|
|
{
|
2023-03-28 10:59:44 +02:00
|
|
|
|
#region Config flags
|
2023-04-02 22:32:26 +02:00
|
|
|
|
/*Configuration: (format: fieldname=value)
|
2023-03-27 10:24:40 +02:00
|
|
|
|
|
2023-04-02 22:32:26 +02:00
|
|
|
|
password_store_path: path to the folder which contains the .gpg files
|
|
|
|
|
|
recipient: whose public key to use for encryption when a new password is created
|
|
|
|
|
|
*/
|
2023-04-16 20:43:25 +02:00
|
|
|
|
private static string CONFIGPATH = SpecialDirectories.CurrentUserApplicationData + "\\config.cfg"; //TODO: make this more unix-friendly
|
2023-04-02 22:32:26 +02:00
|
|
|
|
private static char DELIMETER = '=';
|
|
|
|
|
|
private static string RECIPIENT = "recipient";
|
|
|
|
|
|
private static string PATH = "password_store_path";
|
2023-03-28 10:59:44 +02:00
|
|
|
|
#endregion
|
2023-03-27 10:24:40 +02:00
|
|
|
|
|
2023-04-02 22:32:26 +02:00
|
|
|
|
private static Config Configuration;
|
2023-04-02 22:40:47 +02:00
|
|
|
|
public static event Error? ExceptionOccured;
|
2023-03-27 10:24:40 +02:00
|
|
|
|
|
2023-04-02 22:32:26 +02:00
|
|
|
|
public static void Init()
|
|
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
bool success = false;
|
2023-03-31 11:01:08 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
while (!success)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(CONFIGPATH))
|
2023-03-31 11:01:08 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
StreamReader sr = new StreamReader(CONFIGPATH);
|
|
|
|
|
|
string? path = null, recipient = null;
|
|
|
|
|
|
while (!sr.EndOfStream)
|
2023-03-27 10:24:40 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
string[]? fields = sr.ReadLine().Split(DELIMETER);
|
|
|
|
|
|
if (fields != null)
|
2023-03-27 10:24:40 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
if (fields[0] == PATH)
|
2023-03-31 11:01:31 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
path = fields[1];
|
2023-03-31 11:01:31 +02:00
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
else if (fields[0] == RECIPIENT)
|
2023-03-27 10:24:40 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
recipient = fields[1];
|
2023-03-27 10:24:40 +02:00
|
|
|
|
}
|
2023-03-29 09:25:12 +02:00
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
else //probably an empty line or something
|
2023-03-29 09:25:12 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
continue;
|
2023-03-27 10:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
|
|
|
|
|
|
if (path != null && recipient != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Configuration = new Config(path, recipient);
|
|
|
|
|
|
success = true;
|
|
|
|
|
|
}
|
2023-03-31 11:01:31 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-04-02 22:35:57 +02:00
|
|
|
|
throw new InvalidConfigurationException("One or more required fields were missing from the configuration file.");
|
2023-03-31 11:01:31 +02:00
|
|
|
|
}
|
2023-03-27 10:24:40 +02:00
|
|
|
|
}
|
2023-04-02 22:10:42 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateDefaultConfig();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-02 22:32:26 +02:00
|
|
|
|
}
|
2023-03-27 10:24:40 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
public static string GetPath()
|
|
|
|
|
|
{
|
2023-04-02 22:32:26 +02:00
|
|
|
|
return Configuration.PasswordStorePath;
|
2023-04-02 22:10:42 +02:00
|
|
|
|
}
|
2023-03-27 10:24:40 +02:00
|
|
|
|
|
2023-04-02 22:32:26 +02:00
|
|
|
|
public static string GetRecipient()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Configuration.Recipient;
|
|
|
|
|
|
}
|
2023-03-28 10:59:44 +02:00
|
|
|
|
|
2023-04-02 22:10:42 +02:00
|
|
|
|
private static void CreateDefaultConfig()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2023-03-27 10:24:40 +02:00
|
|
|
|
{
|
2023-04-02 22:10:42 +02:00
|
|
|
|
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
|
|
|
|
|
StreamWriter sw = new StreamWriter(CONFIGPATH);
|
|
|
|
|
|
sw.WriteLine(RECIPIENT + DELIMETER + user);
|
2023-04-16 20:43:25 +02:00
|
|
|
|
sw.WriteLine(PATH + DELIMETER + SpecialDirectories.CurrentUserApplicationData); //TODO: make this unix-friendly
|
2023-04-02 22:10:42 +02:00
|
|
|
|
sw.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2023-04-02 22:40:47 +02:00
|
|
|
|
ExceptionOccured?.Invoke(e);
|
2023-03-27 10:24:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|