2016-03-26 10:10:09 +00:00
|
|
|
|
// 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 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;
|
|
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
|
|
public class SSHAuthenticationInfo : AuthenticationInfo {
|
|
|
|
|
|
2016-03-26 16:52:55 +00:00
|
|
|
|
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 16:52:55 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SSHAuthenticationInfo ()
|
|
|
|
|
{
|
2016-03-26 19:56:24 +00:00
|
|
|
|
Path = IO.Path.Combine (IO.Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath), "ssh");
|
2016-03-27 21:09:57 +00:00
|
|
|
|
|
2016-03-26 10:10:09 +00:00
|
|
|
|
KnownHostsFilePath = IO.Path.Combine (Path, "known_hosts");
|
2016-03-27 21:09:57 +00:00
|
|
|
|
KnownHostsFilePath = MakeWindowsDomainAccountSafe (KnownHostsFilePath);
|
2016-03-26 10:10:09 +00:00
|
|
|
|
|
2016-03-26 18:05:12 +00:00
|
|
|
|
if (IO.Directory.Exists (Path)) {
|
|
|
|
|
ImportKeys ();
|
|
|
|
|
|
|
|
|
|
} else {
|
2016-03-26 10:10:09 +00:00
|
|
|
|
IO.Directory.CreateDirectory (Path);
|
|
|
|
|
CreateKeyPair ();
|
2016-03-26 18:05:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ImportKeys ()
|
|
|
|
|
{
|
|
|
|
|
bool key_found = false;
|
|
|
|
|
|
|
|
|
|
foreach (string file_path in IO.Directory.GetFiles (Path)) {
|
|
|
|
|
if (file_path.EndsWith (".key")) {
|
|
|
|
|
PrivateKeyFilePath = file_path;
|
2016-03-27 10:54:44 +00:00
|
|
|
|
PublicKeyFilePath = file_path + ".pub";
|
2016-03-26 18:05:12 +00:00
|
|
|
|
|
|
|
|
|
key_found = true;
|
|
|
|
|
break;
|
2016-03-26 10:10:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 18:05:12 +00:00
|
|
|
|
|
|
|
|
|
if (key_found) {
|
2016-03-27 21:09:57 +00:00
|
|
|
|
PrivateKeyFilePath = MakeWindowsDomainAccountSafe (PrivateKeyFilePath);
|
|
|
|
|
PublicKeyFilePath = MakeWindowsDomainAccountSafe (PublicKeyFilePath);
|
|
|
|
|
|
2016-03-26 18:05:12 +00:00
|
|
|
|
PrivateKey = IO.File.ReadAllText (PrivateKeyFilePath);
|
2016-03-27 10:54:44 +00:00
|
|
|
|
PublicKey = IO.File.ReadAllText (PublicKeyFilePath);
|
2016-03-26 18:05:12 +00:00
|
|
|
|
|
|
|
|
|
} 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 ();
|
|
|
|
|
|
2016-03-27 10:54:44 +00:00
|
|
|
|
if (computer_name.EndsWith (".local") || computer_name.EndsWith (".config"))
|
|
|
|
|
computer_name = computer_name.Substring (0, computer_name.LastIndexOf ("."));
|
2016-03-26 18:05:12 +00:00
|
|
|
|
|
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 + "\"";
|
|
|
|
|
|
|
|
|
|
var process = new SparkleProcess ("ssh-keygen", arguments);
|
|
|
|
|
process.StartInfo.WorkingDirectory = Path;
|
|
|
|
|
process.Start ();
|
|
|
|
|
process.WaitForExit ();
|
|
|
|
|
|
|
|
|
|
if (process.ExitCode == 0) {
|
|
|
|
|
SparkleLogger.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
|
|
|
|
|
|
|
|
|
SparkleLogger.LogInfo ("Auth", "Could not create key pair");
|
|
|
|
|
return false;
|
2016-03-26 10:10:09 +00:00
|
|
|
|
}
|
2016-03-27 21:09:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use forward slashes in paths when dealing with Windows domain accounts
|
|
|
|
|
string MakeWindowsDomainAccountSafe (string path)
|
|
|
|
|
{
|
|
|
|
|
if (path.StartsWith ("\\\\"))
|
|
|
|
|
return path.Replace ("\\", "/");
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2016-03-26 10:10:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|