From 2114a7dd3d5a9aba50cd274ed76e51ac8f604894 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Fri, 13 Jul 2012 23:48:28 +0200 Subject: [PATCH] controller: Move public key related stuff to its own class --- SparkleShare/Mac/SparkleShare.csproj | 3 + SparkleShare/Makefile.am | 1 + SparkleShare/SparkleControllerBase.cs | 154 +++++------------------ SparkleShare/SparkleKeys.cs | 112 +++++++++++++++++ SparkleShare/SparkleSetupController.cs | 14 ++- SparkleShare/Windows/SparkleShare.csproj | 5 +- 6 files changed, 164 insertions(+), 125 deletions(-) create mode 100644 SparkleShare/SparkleKeys.cs diff --git a/SparkleShare/Mac/SparkleShare.csproj b/SparkleShare/Mac/SparkleShare.csproj index 50903f83..987e1c18 100644 --- a/SparkleShare/Mac/SparkleShare.csproj +++ b/SparkleShare/Mac/SparkleShare.csproj @@ -98,6 +98,9 @@ SparkleInvite.cs + + SparkleKeys.cs + diff --git a/SparkleShare/Makefile.am b/SparkleShare/Makefile.am index 79812a57..823dae1c 100755 --- a/SparkleShare/Makefile.am +++ b/SparkleShare/Makefile.am @@ -18,6 +18,7 @@ SOURCES = \ SparkleEventLogController.cs \ SparkleExtensions.cs \ SparkleInvite.cs \ + SparkleKeys.cs \ SparklePlugin.cs \ SparkleSetupController.cs \ SparkleStatusIconController.cs diff --git a/SparkleShare/SparkleControllerBase.cs b/SparkleShare/SparkleControllerBase.cs index 86bf0976..43d2bf52 100644 --- a/SparkleShare/SparkleControllerBase.cs +++ b/SparkleShare/SparkleControllerBase.cs @@ -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 + private class ActivityDay : List { public DateTime Date; diff --git a/SparkleShare/SparkleKeys.cs b/SparkleShare/SparkleKeys.cs new file mode 100644 index 00000000..cfc63538 --- /dev/null +++ b/SparkleShare/SparkleKeys.cs @@ -0,0 +1,112 @@ +// SparkleShare, a collaboration and sharing tool. +// Copyright (C) 2010 Hylke Bons +// +// 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 . + + +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 ()); + } + } +} diff --git a/SparkleShare/SparkleSetupController.cs b/SparkleShare/SparkleSetupController.cs index 8ebc92cf..b9909968 100755 --- a/SparkleShare/SparkleSetupController.cs +++ b/SparkleShare/SparkleSetupController.cs @@ -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 (); diff --git a/SparkleShare/Windows/SparkleShare.csproj b/SparkleShare/Windows/SparkleShare.csproj index b5fd0df2..30a6ecf2 100644 --- a/SparkleShare/Windows/SparkleShare.csproj +++ b/SparkleShare/Windows/SparkleShare.csproj @@ -109,6 +109,9 @@ SparkleEventLogController.cs + + SparkleKeys.cs + Program.cs @@ -300,4 +303,4 @@ - \ No newline at end of file +