Files
duplicati/Duplicati/Library/Modules/Builtin/ConsolePassphraseInput.cs
T
kenneth.skovhede@gmail.com df085e4271 Update issue #221
Status: Fixed
Added the first generic module, a password request module.
The motivation for this is to allow the creation of custom passphrase modules.
A simple model would be to add a GUI input dialog. 
A more sophisticated version may read a passphrase to an encrypted file which contains the real passphrase or similar.

git-svn-id: https://duplicati.googlecode.com/svn/trunk@519 59da171f-624f-0410-aa54-27559c288bec
2010-09-17 16:20:16 +00:00

101 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Duplicati.Library.Modules.Builtin
{
public class ConsolePassphraseInput : Duplicati.Library.Interface.IGenericModule
{
/// <summary>
/// These actions only use the file list and do not require access to the data inside the files,
/// and thus require no encryption setup
/// </summary>
private readonly static string[] PASSWORDLESS_ACTIONS = { "list", "delete-all-but-n-full", "delete-older-than", "cleanup", "get-backup-sets" };
#region IGenericModule Members
public string Key { get { return "console-password-input"; } }
public string DisplayName { get { return Strings.ConsolePassphraseInput.Displayname; } }
public string Description { get { return Strings.ConsolePassphraseInput.Description; } }
public bool LoadAsDefault { get { return true; } }
public IList<Duplicati.Library.Interface.ICommandLineArgument> SupportedCommands { get { return null; } }
public void Configure(IDictionary<string, string> commandlineOptions)
{
//Ensure the setup is valid, could throw an exception
if (!commandlineOptions.ContainsKey("main-action"))
return;
//First see if a password is actually required for the action
foreach (string s in PASSWORDLESS_ACTIONS)
if (string.Equals(s, commandlineOptions["main-action"], StringComparison.InvariantCultureIgnoreCase))
return;
//See if a password is already present or encryption is disabled
if (!commandlineOptions.ContainsKey("passphrase") && !commandlineOptions.ContainsKey("no-encryption"))
{
//Get the passphrase
bool confirm = string.Equals(commandlineOptions["main-action"], "backup", StringComparison.InvariantCultureIgnoreCase);
commandlineOptions["passphrase"] = ReadPassphraseFromConsole(confirm);
}
}
#endregion
private static string ReadPassphraseFromConsole(bool confirm)
{
Console.Write("\n" + Strings.ConsolePassphraseInput.EnterPassphrasePrompt + ": ");
StringBuilder passphrase = new StringBuilder();
while (true)
{
ConsoleKeyInfo k = Console.ReadKey(true);
if (k.Key == ConsoleKey.Enter)
break;
if (k.Key == ConsoleKey.Escape)
throw new Library.Interface.CancelException("");
passphrase.Append(k.KeyChar);
//Unix/Linux user know that there is no feedback, Win user gets scared :)
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
Console.Write("*");
}
Console.WriteLine();
if (confirm)
{
Console.Write("\n" + Strings.ConsolePassphraseInput.ConfirmPassphrasePrompt + ": ");
StringBuilder password2 = new StringBuilder();
while (true)
{
ConsoleKeyInfo k = Console.ReadKey(true);
if (k.Key == ConsoleKey.Enter)
break;
if (k.Key == ConsoleKey.Escape)
return null;
password2.Append(k.KeyChar);
//Unix/Linux user know that there is no feedback, Win user gets scared :)
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
Console.Write("*");
}
Console.WriteLine();
if (passphrase.ToString() != password2.ToString())
throw new Exception(Strings.ConsolePassphraseInput.PassphraseMismatchError);
}
if (passphrase.ToString().Length == 0)
throw new Exception(Strings.ConsolePassphraseInput.EmptyPassphraseError);
return passphrase.ToString();
}
}
}