controller: Move public key related stuff to its own class

This commit is contained in:
Hylke Bons 2012-07-13 23:48:28 +02:00
parent 90f1c12f3d
commit 2114a7dd3d
6 changed files with 164 additions and 125 deletions

View file

@ -98,6 +98,9 @@
<Compile Include="..\SparkleInvite.cs">
<Link>SparkleInvite.cs</Link>
</Compile>
<Compile Include="..\SparkleKeys.cs">
<Link>SparkleKeys.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="MainMenu.xib" xmlns="" />

View file

@ -18,6 +18,7 @@ SOURCES = \
SparkleEventLogController.cs \
SparkleExtensions.cs \
SparkleInvite.cs \
SparkleKeys.cs \
SparklePlugin.cs \
SparkleSetupController.cs \
SparkleStatusIconController.cs

View file

@ -304,10 +304,37 @@ namespace SparkleShare {
if (CreateSparkleShareFolder ())
AddToBookmarks ();
if (FirstRun)
if (FirstRun) {
SparkleConfig.DefaultConfig.SetConfigOption ("notifications", bool.TrueString);
else
ImportPrivateKey ();
} else {
string keys_path = Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath);
string key_file_name = "sparkleshare." + CurrentUser.Email + ".key";
string key_file_path = Path.Combine (keys_path, key_file_name);
// Be forgiving about the key's file name
if (!File.Exists (key_file_path)) {
foreach (string file_name in Directory.GetFiles (keys_path)) {
if (file_name.StartsWith ("sparkleshare") &&
file_name.EndsWith (".key")) {
key_file_path = Path.Combine (keys_path, file_name);
break;
}
}
}
string pubkey_file_path = key_file_path + ".pub";
string link_code_file_path = Path.Combine (SparklePath, CurrentUser.Name + "'s link code.txt");
// Create an easily accessible copy of the public
// key in the user's SparkleShare folder
if (File.Exists (pubkey_file_path) && !File.Exists (link_code_file_path))
File.Copy (pubkey_file_path, link_code_file_path, true /* Overwriting allowed */ );
SparkleKeys.ImportPrivateKey (key_file_path);
SparkleKeys.ListPrivateKeys ();
}
// Watch the SparkleShare folder
FileSystemWatcher watcher = new FileSystemWatcher () {
@ -573,7 +600,7 @@ namespace SparkleShare {
try {
repo = (SparkleRepoBase) Activator.CreateInstance (
Type.GetType ("SparkleLib." + backend + ".SparkleRepo, SparkleLib." + backend),
folder_path
new object [] {folder_path, SparkleConfig.DefaultConfig}
);
} catch {
@ -691,123 +718,6 @@ namespace SparkleShare {
OpenFolder (new SparkleFolder (name).FullPath);
}
// Adds the user's SparkleShare key to the ssh-agent,
// so all activity is done with this key
public void ImportPrivateKey ()
{
string keys_path = Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath);
string key_file_name = "sparkleshare." + CurrentUser.Email + ".key";
string key_file_path = Path.Combine (keys_path, key_file_name);
if (!File.Exists (key_file_path)) {
foreach (string file_name in Directory.GetFiles (keys_path)) {
if (file_name.StartsWith ("sparkleshare") &&
file_name.EndsWith (".key")) {
key_file_path = Path.Combine (keys_path, file_name);
break;
}
}
}
Process process = new Process ();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = "\"" + key_file_path + "\"";
process.StartInfo.CreateNoWindow = true;
process.Start ();
process.WaitForExit ();
string pubkey_file_path = key_file_path + ".pub";
// Create an easily accessible copy of the public
// key in the user's SparkleShare folder
if (!File.Exists (pubkey_file_path))
File.Copy (pubkey_file_path,
Path.Combine (SparklePath, CurrentUser.Name + "'s key.txt"), true); // Overwriting is allowed
ListPrivateKeys ();
}
private void ListPrivateKeys ()
{
Process process = new Process () {
EnableRaisingEvents = true
};
process.StartInfo.WorkingDirectory = SparkleConfig.DefaultConfig.TmpPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = "-l";
process.Start ();
// Reading the standard output HAS to go before
// WaitForExit, or it will hang forever on output > 4096 bytes
string keys_in_use = process.StandardOutput.ReadToEnd ().Trim ();
process.WaitForExit ();
SparkleHelpers.DebugInfo ("Auth",
"The following keys will be available to SparkleShare: " + Environment.NewLine + keys_in_use);
}
// Generates and installs an RSA keypair to identify this system
public void GenerateKeyPair ()
{
string keys_path = Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath);
string key_file_name = "sparkleshare." + CurrentUser.Email + ".key";
string key_file_path = Path.Combine (keys_path, key_file_name);
if (File.Exists (key_file_path)) {
SparkleHelpers.DebugInfo ("Auth", "Keypair exists ('" + key_file_name + "'), leaving it untouched");
return;
} else {
if (!Directory.Exists (keys_path))
Directory.CreateDirectory (keys_path);
}
Process process = new Process () {
EnableRaisingEvents = true
};
process.StartInfo.WorkingDirectory = keys_path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "ssh-keygen";
process.StartInfo.CreateNoWindow = true;
string computer_name = System.Net.Dns.GetHostName ();
if (computer_name.EndsWith (".local"))
computer_name = computer_name.Replace (".local", "");
process.StartInfo.Arguments = "-t rsa " + // crypto type
"-P \"\" " + // password (none)
"-C \"" + computer_name + "\" " + // key comment
"-f " + key_file_name; // file name
process.Start ();
process.WaitForExit ();
if (process.ExitCode == 0)
SparkleHelpers.DebugInfo ("Auth", "Created keypair '" + key_file_name + "'");
else
SparkleHelpers.DebugInfo ("Auth", "Could not create keypair '" + key_file_name + "'");
// Create an easily accessible copy of the public
// key in the user's SparkleShare folder
File.Copy (key_file_path + ".pub", Path.Combine (SparklePath, CurrentUser.Name + "'s link code.txt"), true);
}
public void StartFetcher (string address, string required_fingerprint,
string remote_path, string announcements_url, bool fetch_prior_history)
@ -1097,7 +1007,7 @@ namespace SparkleShare {
// All change sets that happened on a day
private class ActivityDay : List <SparkleChangeSet>
private class ActivityDay : List<SparkleChangeSet>
{
public DateTime Date;

112
SparkleShare/SparkleKeys.cs Normal file
View file

@ -0,0 +1,112 @@
// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using SparkleLib;
namespace SparkleShare {
public static class SparkleKeys {
public static string GenerateKeyPair (string output_path, string key_name)
{
key_name += ".key";
string key_file_path = Path.Combine (output_path, key_name);
if (File.Exists (key_file_path)) {
SparkleHelpers.DebugInfo ("Auth", "A key pair exists ('" + key_name + "'), leaving it untouched");
return key_file_path;
} else {
if (!Directory.Exists (output_path))
Directory.CreateDirectory (output_path);
}
Process process = new Process ();
process.StartInfo.FileName = "ssh-keygen";
process.StartInfo.WorkingDirectory = output_path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
string computer_name = Dns.GetHostName ();
if (computer_name.EndsWith (".local"))
computer_name = computer_name.Substring (0, computer_name.Length - 6);
process.StartInfo.Arguments = "-t rsa " + // crypto type
"-P \"\"" /* password (none) */ + " " +
"-C \"" + computer_name + "\"" /* key comment */ + " " +
"-f \"" + key_name + "\"" /* file name */;
process.Start ();
process.WaitForExit ();
if (process.ExitCode == 0)
SparkleHelpers.DebugInfo ("Auth", "Created keypair '" + key_file_path + "'");
else
SparkleHelpers.DebugInfo ("Auth", "Could not create key pair '" + key_file_path + "'");
return key_file_path;
}
public static void ImportPrivateKey (string key_file_path)
{
Process process = new Process ();
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = "\"" + key_file_path + "\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start ();
process.WaitForExit ();
if (process.ExitCode == 0)
SparkleHelpers.DebugInfo ("Auth", "Imported key '" + key_file_path + "'");
else
SparkleHelpers.DebugInfo ("Auth", "Could not import key '" + key_file_path + "'");
}
public static void ListPrivateKeys ()
{
Process process = new Process ();
process.StartInfo.FileName = "ssh-add";
process.StartInfo.Arguments = "-l";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start ();
// Reading the standard output HAS to go before
// WaitForExit, or it will hang forever on output > 4096 bytes
string keys_in_use = process.StandardOutput.ReadToEnd ();
process.WaitForExit ();
SparkleHelpers.DebugInfo ("Auth", "The following keys will be available to SparkleShare: " +
Environment.NewLine + keys_in_use.Trim ());
}
}
}

View file

@ -283,8 +283,18 @@ namespace SparkleShare {
new Thread (
new ThreadStart (delegate {
Program.Controller.GenerateKeyPair ();
Program.Controller.ImportPrivateKey ();
string keys_path = Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath);
string key_file_name = "sparkleshare." + Program.Controller.CurrentUser.Email;
string private_key_file_path = SparkleKeys.GenerateKeyPair (keys_path, key_file_name);
SparkleKeys.ImportPrivateKey (private_key_file_path);
string link_code_file_path = Path.Combine (Program.Controller.SparklePath,
Program.Controller.CurrentUser.Name + "'s link code.txt");
// Create an easily accessible copy of the public
// key in the user's SparkleShare folder
File.Copy (private_key_file_path + ".pub", link_code_file_path, true);
})
).Start ();

View file

@ -109,6 +109,9 @@
<Compile Include="..\SparkleEventLogController.cs">
<Link>SparkleEventLogController.cs</Link>
</Compile>
<Compile Include="..\SparkleKeys.cs">
<Link>SparkleKeys.cs</Link>
</Compile>
<Compile Include="SparkleSetupWindow.cs" />
<Compile Include="..\Program.cs">
<Link>Program.cs</Link>
@ -300,4 +303,4 @@
<EmbeddedResource Include="Pixmaps\tutorial-slide-2.png" />
<EmbeddedResource Include="Pixmaps\tutorial-slide-3.png" />
</ItemGroup>
</Project>
</Project>