2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-16 20:19:58 +00:00
|
|
|
// 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;
|
2012-04-12 22:44:31 +00:00
|
|
|
using System.Collections.Generic;
|
2012-04-11 18:07:34 +00:00
|
|
|
using System.Diagnostics;
|
2012-01-31 21:30:23 +00:00
|
|
|
using System.IO;
|
2012-04-12 22:44:31 +00:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Text;
|
2011-05-31 01:26:57 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2011-05-16 20:19:58 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
// Sets up a fetcher that can get remote folders
|
|
|
|
public abstract class SparkleFetcherBase {
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event Action Started = delegate { };
|
|
|
|
public event Action Failed = delegate { };
|
|
|
|
|
|
|
|
public event FinishedEventHandler Finished = delegate { };
|
2012-04-22 12:32:55 +00:00
|
|
|
public delegate void FinishedEventHandler (bool repo_is_encrypted, bool repo_is_empty, string [] warnings);
|
2012-07-18 10:35:22 +00:00
|
|
|
|
|
|
|
public event ProgressChangedEventHandler ProgressChanged = delegate { };
|
2011-08-27 15:53:17 +00:00
|
|
|
public delegate void ProgressChangedEventHandler (double percentage);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
public abstract bool Fetch ();
|
|
|
|
public abstract void Stop ();
|
2012-04-22 12:32:55 +00:00
|
|
|
public abstract bool IsFetchedRepoEmpty { get; }
|
|
|
|
public abstract bool IsFetchedRepoPasswordCorrect (string password);
|
|
|
|
public abstract void EnableFetchedRepoCrypto (string password);
|
|
|
|
|
|
|
|
public Uri RemoteUrl { get; protected set; }
|
|
|
|
public string RequiredFingerprint { get; protected set; }
|
|
|
|
public readonly bool FetchPriorHistory = false;
|
|
|
|
public string TargetFolder { get; protected set; }
|
2012-02-12 21:25:20 +00:00
|
|
|
public bool IsActive { get; private set; }
|
2012-07-06 09:26:02 +00:00
|
|
|
public string Identifier;
|
2011-06-05 16:35:22 +00:00
|
|
|
|
2012-04-22 12:32:55 +00:00
|
|
|
public string [] Warnings {
|
|
|
|
get {
|
|
|
|
return this.warnings.ToArray ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 20:56:27 +00:00
|
|
|
public string [] Errors {
|
|
|
|
get {
|
|
|
|
return this.errors.ToArray ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 09:43:18 +00:00
|
|
|
|
2012-04-22 12:32:55 +00:00
|
|
|
protected List<string> warnings = new List<string> ();
|
2012-07-06 09:26:02 +00:00
|
|
|
protected List<string> errors = new List<string> ();
|
|
|
|
|
|
|
|
protected string [] ExcludeRules = new string [] {
|
|
|
|
"*.autosave", // Various autosaving apps
|
|
|
|
"*~", // gedit and emacs
|
|
|
|
".~lock.*", // LibreOffice
|
|
|
|
"*.part", "*.crdownload", // Firefox and Chromium temporary download files
|
|
|
|
".*.sw[a-z]", "*.un~", "*.swp", "*.swo", // vi(m)
|
|
|
|
".directory", // KDE
|
|
|
|
".DS_Store", "Icon\r\r", "._*", ".Spotlight-V100", ".Trashes", // Mac OS X
|
|
|
|
"*(Autosaved).graffle", // Omnigraffle
|
|
|
|
"Thumbs.db", "Desktop.ini", // Windows
|
|
|
|
"~*.tmp", "~*.TMP", "*~*.tmp", "*~*.TMP", // MS Office
|
|
|
|
"~*.ppt", "~*.PPT", "~*.pptx", "~*.PPTX",
|
|
|
|
"~*.xls", "~*.XLS", "~*.xlsx", "~*.XLSX",
|
|
|
|
"~*.doc", "~*.DOC", "~*.docx", "~*.DOCX",
|
|
|
|
"*/CVS/*", ".cvsignore", "*/.cvsignore", // CVS
|
|
|
|
"/.svn/*", "*/.svn/*", // Subversion
|
|
|
|
"/.hg/*", "*/.hg/*", "*/.hgignore", // Mercurial
|
|
|
|
"/.bzr/*", "*/.bzr/*", "*/.bzrignore" // Bazaar
|
|
|
|
};
|
|
|
|
|
2012-06-17 20:56:27 +00:00
|
|
|
|
2011-08-27 18:57:34 +00:00
|
|
|
private Thread thread;
|
2011-05-16 20:19:58 +00:00
|
|
|
|
2012-01-31 21:30:23 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
public SparkleFetcherBase (string server, string required_fingerprint,
|
|
|
|
string remote_path, string target_folder, bool fetch_prior_history)
|
2011-05-16 20:19:58 +00:00
|
|
|
{
|
2012-04-12 22:44:31 +00:00
|
|
|
RequiredFingerprint = required_fingerprint;
|
|
|
|
FetchPriorHistory = fetch_prior_history;
|
|
|
|
remote_path = remote_path.Trim ("/".ToCharArray ());
|
2012-04-11 21:10:02 +00:00
|
|
|
|
|
|
|
if (server.EndsWith ("/"))
|
|
|
|
server = server.Substring (0, server.Length - 1);
|
|
|
|
|
|
|
|
if (!remote_path.StartsWith ("/"))
|
|
|
|
remote_path = "/" + remote_path;
|
|
|
|
|
|
|
|
if (!server.Contains ("://"))
|
|
|
|
server = "ssh://" + server;
|
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
TargetFolder = target_folder;
|
2012-04-11 21:10:02 +00:00
|
|
|
RemoteUrl = new Uri (server + remote_path);
|
|
|
|
IsActive = false;
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Start ()
|
|
|
|
{
|
2012-02-12 21:25:20 +00:00
|
|
|
IsActive = true;
|
2012-07-18 10:35:22 +00:00
|
|
|
Started ();
|
2011-05-16 20:19:58 +00:00
|
|
|
|
2012-06-29 11:53:13 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", TargetFolder + " | Fetching folder: " + RemoteUrl);
|
2012-04-12 22:44:31 +00:00
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
if (Directory.Exists (TargetFolder))
|
|
|
|
Directory.Delete (TargetFolder, true);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
2012-04-22 12:32:55 +00:00
|
|
|
string host = RemoteUrl.Host;
|
2012-04-11 18:07:34 +00:00
|
|
|
string host_key = GetHostKey ();
|
2012-04-11 21:10:02 +00:00
|
|
|
|
2012-04-22 12:32:55 +00:00
|
|
|
if (string.IsNullOrEmpty (host) || host_key == null) {
|
2012-07-18 10:35:22 +00:00
|
|
|
Failed ();
|
2012-04-12 22:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool warn = true;
|
|
|
|
if (RequiredFingerprint != null) {
|
|
|
|
string host_fingerprint = GetFingerprint (host_key);
|
|
|
|
|
2012-04-26 17:48:18 +00:00
|
|
|
if (host_fingerprint == null ||
|
|
|
|
!RequiredFingerprint.Equals (host_fingerprint)) {
|
|
|
|
|
2012-04-12 22:44:31 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Auth", "Fingerprint doesn't match");
|
2012-07-18 10:35:22 +00:00
|
|
|
Failed ();
|
2012-04-12 22:44:31 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
warn = false;
|
|
|
|
SparkleHelpers.DebugInfo ("Auth", "Fingerprint matches");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
SparkleHelpers.DebugInfo ("Auth", "Skipping fingerprint check");
|
|
|
|
}
|
|
|
|
|
|
|
|
AcceptHostKey (host_key, warn);
|
2012-04-11 18:07:34 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
this.thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
if (Fetch ()) {
|
|
|
|
Thread.Sleep (500);
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Finished");
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
IsActive = false;
|
2011-05-16 20:19:58 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
// TODO: Find better way to determine if folder should have crypto setup
|
|
|
|
bool repo_is_encrypted = RemoteUrl.ToString ().Contains ("crypto");
|
2012-07-18 10:35:22 +00:00
|
|
|
Finished (repo_is_encrypted, IsFetchedRepoEmpty, Warnings);
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
} else {
|
|
|
|
Thread.Sleep (500);
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Failed");
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
IsActive = false;
|
2012-07-18 10:35:22 +00:00
|
|
|
Failed ();
|
2012-07-06 09:26:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2011-05-16 20:19:58 +00:00
|
|
|
|
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
public virtual void Complete ()
|
|
|
|
{
|
|
|
|
string identifier_path = Path.Combine (TargetFolder, ".sparkleshare");
|
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
if (File.Exists (identifier_path)) {
|
|
|
|
Identifier = File.ReadAllText (identifier_path).Trim ();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Identifier = CreateIdentifier ();
|
2012-07-05 11:33:32 +00:00
|
|
|
File.WriteAllText (identifier_path, Identifier);
|
2012-07-06 09:26:02 +00:00
|
|
|
}
|
2012-07-05 11:33:32 +00:00
|
|
|
|
|
|
|
if (IsFetchedRepoEmpty)
|
|
|
|
CreateInitialChangeSet ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create an initial change set when the
|
|
|
|
// user has fetched an empty remote folder
|
|
|
|
public void CreateInitialChangeSet ()
|
|
|
|
{
|
|
|
|
string file_path = Path.Combine (TargetFolder, "SparkleShare.txt");
|
|
|
|
string n = Environment.NewLine;
|
|
|
|
|
|
|
|
string text = "Congratulations, you've successfully created a SparkleShare repository!" + n +
|
|
|
|
n +
|
|
|
|
"Any files you add or change in this folder will be automatically synced to " + n +
|
|
|
|
RemoteUrl + " and everyone connected to it." + n +
|
|
|
|
n +
|
|
|
|
"SparkleShare is an Open Source software program that helps people " + n +
|
|
|
|
"collaborate and share files. If you like what we do, please consider a small " + n +
|
|
|
|
"donation to support the project: http://sparkleshare.org/support-us/" + n +
|
|
|
|
n +
|
|
|
|
"Have fun! :)" + n;
|
|
|
|
|
|
|
|
File.WriteAllText (file_path, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
public static string CreateIdentifier ()
|
|
|
|
{
|
2012-07-08 09:29:00 +00:00
|
|
|
string random = Path.GetRandomFileName ();
|
|
|
|
return SparkleHelpers.SHA1 (random);
|
2012-07-06 09:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetBackend (string path)
|
|
|
|
{
|
|
|
|
string extension = Path.GetExtension (path);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty (extension)) {
|
|
|
|
extension = extension.Substring (1);
|
|
|
|
char [] letters = extension.ToCharArray ();
|
|
|
|
letters [0] = char.ToUpper (letters [0]);
|
|
|
|
|
|
|
|
return new string (letters);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return "Git";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-16 20:19:58 +00:00
|
|
|
public void Dispose ()
|
|
|
|
{
|
2011-06-05 16:35:22 +00:00
|
|
|
if (this.thread != null) {
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
|
|
|
}
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
2012-07-05 11:33:32 +00:00
|
|
|
|
|
|
|
|
2011-08-27 15:53:17 +00:00
|
|
|
protected void OnProgressChanged (double percentage) {
|
2012-07-18 10:35:22 +00:00
|
|
|
ProgressChanged (percentage);
|
2011-08-27 15:53:17 +00:00
|
|
|
}
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2011-06-05 18:59:48 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
private string GetHostKey ()
|
2011-05-31 01:26:57 +00:00
|
|
|
{
|
2012-04-11 18:07:34 +00:00
|
|
|
string host = RemoteUrl.Host;
|
|
|
|
SparkleHelpers.DebugInfo ("Auth", "Fetching host key for " + host);
|
2011-06-05 18:59:48 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
Process process = new Process () {
|
|
|
|
EnableRaisingEvents = true
|
|
|
|
};
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
process.StartInfo.WorkingDirectory = SparkleConfig.DefaultConfig.TmpPath;
|
|
|
|
process.StartInfo.UseShellExecute = false;
|
|
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
|
|
process.StartInfo.CreateNoWindow = true;
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
process.StartInfo.FileName = "ssh-keyscan";
|
2012-04-11 18:07:34 +00:00
|
|
|
process.StartInfo.Arguments = "-t rsa " + host;
|
2011-10-30 21:22:48 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
process.Start ();
|
2011-10-30 21:22:48 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
// Reading the standard output HAS to go before
|
|
|
|
// WaitForExit, or it will hang forever on output > 4096 bytes
|
2012-04-12 22:44:31 +00:00
|
|
|
string host_key = process.StandardOutput.ReadToEnd ().Trim ();
|
2012-04-11 18:07:34 +00:00
|
|
|
process.WaitForExit ();
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
if (process.ExitCode == 0)
|
|
|
|
return host_key;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
2011-10-30 21:22:48 +00:00
|
|
|
|
|
|
|
|
2012-04-12 22:44:31 +00:00
|
|
|
// FIXME: Calculate fingerprint natively: decode base64 -> md5
|
|
|
|
private string GetFingerprint (string public_key)
|
|
|
|
{
|
|
|
|
string tmp_file_path = Path.Combine (SparkleConfig.DefaultConfig.TmpPath, "hostkey.tmp");
|
|
|
|
File.WriteAllText (tmp_file_path, public_key + Environment.NewLine);
|
|
|
|
|
|
|
|
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-keygen";
|
2012-06-28 10:57:10 +00:00
|
|
|
process.StartInfo.Arguments = "-lf \"" + tmp_file_path + "\"";
|
2012-04-12 22:44:31 +00:00
|
|
|
|
|
|
|
process.Start ();
|
|
|
|
|
|
|
|
// Reading the standard output HAS to go before
|
|
|
|
// WaitForExit, or it will hang forever on output > 4096 bytes
|
|
|
|
string fingerprint = process.StandardOutput.ReadToEnd ().Trim ();
|
|
|
|
process.WaitForExit ();
|
2012-07-06 09:26:02 +00:00
|
|
|
|
2012-06-28 11:03:04 +00:00
|
|
|
File.Delete (tmp_file_path);
|
2012-04-12 22:44:31 +00:00
|
|
|
|
2012-04-26 17:48:18 +00:00
|
|
|
try {
|
|
|
|
fingerprint = fingerprint.Substring (fingerprint.IndexOf (" ") + 1, 47);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
SparkleHelpers.DebugInfo ("Fetcher", "Not a valid fingerprint: " + e.Message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:44:31 +00:00
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AcceptHostKey (string host_key, bool warn)
|
2012-04-11 18:07:34 +00:00
|
|
|
{
|
|
|
|
string ssh_config_path = Path.Combine (SparkleConfig.DefaultConfig.HomePath, ".ssh");
|
|
|
|
string known_hosts_file_path = Path.Combine (ssh_config_path, "known_hosts");
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
if (!File.Exists (known_hosts_file_path)) {
|
|
|
|
if (!Directory.Exists (ssh_config_path))
|
|
|
|
Directory.CreateDirectory (ssh_config_path);
|
2011-05-31 01:26:57 +00:00
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
File.Create (known_hosts_file_path).Close ();
|
2011-05-31 01:26:57 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 19:31:18 +00:00
|
|
|
string host = RemoteUrl.Host;
|
|
|
|
string known_hosts = File.ReadAllText (known_hosts_file_path);
|
|
|
|
string [] known_hosts_lines = File.ReadAllLines (known_hosts_file_path);
|
|
|
|
|
|
|
|
foreach (string line in known_hosts_lines) {
|
|
|
|
if (line.StartsWith (host + " "))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (known_hosts.EndsWith ("\n"))
|
2012-04-11 18:07:34 +00:00
|
|
|
File.AppendAllText (known_hosts_file_path, host_key + "\n");
|
2011-05-31 01:26:57 +00:00
|
|
|
else
|
2012-04-11 18:07:34 +00:00
|
|
|
File.AppendAllText (known_hosts_file_path, "\n" + host_key + "\n");
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Auth", "Accepted host key for " + host);
|
2012-04-12 22:44:31 +00:00
|
|
|
|
2012-04-15 10:35:17 +00:00
|
|
|
if (warn)
|
2012-04-22 12:32:55 +00:00
|
|
|
this.warnings.Add ("The following host key has been accepted:\n" + GetFingerprint (host_key));
|
2011-12-25 19:33:39 +00:00
|
|
|
}
|
2011-05-16 20:19:58 +00:00
|
|
|
}
|
|
|
|
}
|