SparkleShare/Sparkles/SSHAuthenticationInfo.cs

129 lines
4.3 KiB
C#
Raw Normal View History

2016-03-26 10:10:09 +00:00
// SparkleShare, a collaboration and sharing tool.
2017-07-23 12:47:54 +00:00
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
2016-03-26 10:10:09 +00:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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.Net;
using IO = System.IO;
2016-03-31 08:35:26 +00:00
namespace Sparkles {
2016-03-26 10:10:09 +00:00
public class SSHAuthenticationInfo : AuthenticationInfo {
public static SSHAuthenticationInfo DefaultAuthenticationInfo;
2016-03-26 19:56:24 +00:00
public string PrivateKeyFilePath { get; private set; }
public string PrivateKey { get; private set; }
2016-03-26 19:56:24 +00:00
public string PublicKeyFilePath { get; private set; }
public string PublicKey { get; private set; }
2016-03-26 10:10:09 +00:00
2016-03-26 19:56:24 +00:00
public string KnownHostsFilePath { get; private set; }
2016-03-26 10:10:09 +00:00
2016-03-26 19:56:24 +00:00
readonly string Path;
2016-03-26 10:10:09 +00:00
2016-03-26 10:10:09 +00:00
public SSHAuthenticationInfo ()
{
Path = IO.Path.Combine (Configuration.DefaultConfiguration.DirectoryPath, "ssh");
2016-03-26 10:10:09 +00:00
KnownHostsFilePath = IO.Path.Combine (Path, "known_hosts");
KnownHostsFilePath = MakeWindowsDomainAccountSafe (KnownHostsFilePath);
2016-03-26 10:10:09 +00:00
if (IO.Directory.Exists (Path)) {
ImportKeys ();
} else {
2016-03-26 10:10:09 +00:00
IO.Directory.CreateDirectory (Path);
CreateKeyPair ();
}
}
void ImportKeys ()
{
bool key_found = false;
foreach (string file_path in IO.Directory.GetFiles (Path)) {
if (file_path.EndsWith (".key", StringComparison.InvariantCultureIgnoreCase)) {
PrivateKeyFilePath = file_path;
2016-03-27 10:54:44 +00:00
PublicKeyFilePath = file_path + ".pub";
key_found = true;
break;
2016-03-26 10:10:09 +00:00
}
}
if (key_found) {
PrivateKeyFilePath = MakeWindowsDomainAccountSafe (PrivateKeyFilePath);
PublicKeyFilePath = MakeWindowsDomainAccountSafe (PublicKeyFilePath);
PrivateKey = IO.File.ReadAllText (PrivateKeyFilePath);
2016-03-27 10:54:44 +00:00
PublicKey = IO.File.ReadAllText (PublicKeyFilePath);
} else {
CreateKeyPair ();
ImportKeys ();
}
2016-03-26 10:10:09 +00:00
}
bool CreateKeyPair ()
{
string key_file_name = DateTime.Now.ToString ("yyyy-MM-dd_HH\\hmm") + ".key";
string computer_name = Dns.GetHostName ();
if (computer_name.EndsWith (".local", StringComparison.InvariantCultureIgnoreCase) ||
computer_name.EndsWith (".config", StringComparison.InvariantCultureIgnoreCase))
computer_name = computer_name.Substring (0,
computer_name.LastIndexOf (".", StringComparison.InvariantCulture));
2016-03-26 10:10:09 +00:00
string arguments =
"-t rsa " + // Crypto type
"-b 4096 " + // Key size
"-P \"\" " + // No password
"-C \"" + computer_name + " (SparkleShare)\" " + // Key comment
"-f \"" + key_file_name + "\"";
2016-10-25 09:25:34 +00:00
var ssh_keygen = new SSHCommand ("ssh-keygen", arguments);
2016-04-09 15:32:01 +00:00
ssh_keygen.StartInfo.WorkingDirectory = Path;
ssh_keygen.StartAndWaitForExit ();
2016-03-26 10:10:09 +00:00
2016-04-09 15:32:01 +00:00
if (ssh_keygen.ExitCode == 0) {
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Auth", "Created key pair: " + key_file_name);
2016-03-26 19:56:24 +00:00
ImportKeys ();
2016-03-26 10:10:09 +00:00
2016-03-26 19:56:24 +00:00
return true;
2016-03-26 10:10:09 +00:00
}
2016-03-26 19:56:24 +00:00
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Auth", "Could not create key pair");
2016-03-26 19:56:24 +00:00
return false;
2016-03-26 10:10:09 +00:00
}
// Use forward slashes in paths when dealing with Windows domain accounts
string MakeWindowsDomainAccountSafe (string path)
{
if (path.StartsWith ("\\\\", StringComparison.InvariantCulture))
return path.Replace ("\\", "/");
return path;
}
2016-03-26 10:10:09 +00:00
}
}