auth: Pass SSH auth to Git command through GIT_SSH_COMMAND

This commit is contained in:
Hylke Bons 2016-03-26 18:05:12 +00:00
parent 47d821ad2a
commit b76b47f64e
2 changed files with 50 additions and 26 deletions

View file

@ -13,6 +13,7 @@
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace SparkleLib.Git { namespace SparkleLib.Git {
@ -32,27 +33,32 @@ namespace SparkleLib.Git {
StartInfo.FileName = GitPath; StartInfo.FileName = GitPath;
StartInfo.WorkingDirectory = path; StartInfo.WorkingDirectory = path;
if (StartInfo.EnvironmentVariables.ContainsKey ("LANG")) if (string.IsNullOrEmpty (SSHPath))
StartInfo.EnvironmentVariables ["LANG"] = "en_US"; SSHPath = "ssh";
else
StartInfo.EnvironmentVariables.Add ("LANG", "en_US");
if (StartInfo.EnvironmentVariables.ContainsKey ("GIT_TERMINAL_PROMPT")) string GIT_SSH_COMMAND = SSHPath + " " +
StartInfo.EnvironmentVariables ["GIT_TERMINAL_PROMPT"] = "0"; "-o UserKnownHostsFile=" + SSHAuthenticationInfo.DefaultAuthenticationInfo.KnownHostsFilePath + " " +
else "-o PasswordAuthentication=no " +
StartInfo.EnvironmentVariables.Add ("GIT_TERMINAL_PROMPT", "0"); "-F /dev/null " + // Ignore the environment's SSH config file
"-i " + SSHAuthenticationInfo.DefaultAuthenticationInfo.PrivateKeyFilePath;
if (!string.IsNullOrEmpty (SSHPath)) { SetEnvironmentVariable ("GIT_SSH_COMMAND", GIT_SSH_COMMAND);
if (StartInfo.EnvironmentVariables.ContainsKey ("GIT_SSH_COMMAND")) SetEnvironmentVariable ("LANG", "en_US");
StartInfo.EnvironmentVariables ["GIT_SSH_COMMAND"] = SSHPath; SetEnvironmentVariable ("GIT_TERMINAL_PROMPT", "0");
else
StartInfo.EnvironmentVariables.Add ("GIT_SSH_COMMAND", SSHPath);
}
if (string.IsNullOrEmpty (ExecPath)) if (string.IsNullOrEmpty (ExecPath))
StartInfo.Arguments = args; StartInfo.Arguments = args;
else else
StartInfo.Arguments = "--exec-path=\"" + ExecPath + "\" " + args; StartInfo.Arguments = "--exec-path=\"" + ExecPath + "\" " + args;
} }
void SetEnvironmentVariable (string variable, string content)
{
if (StartInfo.EnvironmentVariables.ContainsKey (variable))
StartInfo.EnvironmentVariables [variable] = content;
else
StartInfo.EnvironmentVariables.Add (variable, content);
}
} }
} }

View file

@ -48,23 +48,38 @@ namespace SparkleLib {
KnownHostsFilePath = IO.Path.Combine (Path, "known_hosts"); KnownHostsFilePath = IO.Path.Combine (Path, "known_hosts");
if (!IO.Directory.Exists (Path)) { if (IO.Directory.Exists (Path)) {
ImportKeys ();
} else {
IO.Directory.CreateDirectory (Path); IO.Directory.CreateDirectory (Path);
CreateKeyPair (); CreateKeyPair ();
}
} else { }
foreach (string file_path in IO.Directory.GetFiles (Path)) {
if (file_path.EndsWith (".key")) {
PrivateKeyFilePath = file_path;
PrivateKey = IO.File.ReadAllText (file_path);
}
if (file_path.EndsWith (".key.pub")) {
PublicKeyFilePath = file_path; void ImportKeys ()
PublicKey = IO.File.ReadAllText (file_path); {
} bool key_found = false;
foreach (string file_path in IO.Directory.GetFiles (Path)) {
if (file_path.EndsWith (".key")) {
PrivateKeyFilePath = file_path;
PublicKeyFilePath = file_path + ".pub";
key_found = true;
break;
} }
} }
if (key_found) {
PrivateKey = IO.File.ReadAllText (PrivateKeyFilePath);
PublicKey = IO.File.ReadAllText (PublicKeyFilePath);
} else {
CreateKeyPair ();
ImportKeys ();
}
} }
@ -77,6 +92,9 @@ namespace SparkleLib {
if (computer_name.EndsWith (".local")) if (computer_name.EndsWith (".local"))
computer_name = computer_name.Substring (0, computer_name.Length - ".local".Length); computer_name = computer_name.Substring (0, computer_name.Length - ".local".Length);
if (computer_name.EndsWith (".config"))
computer_name = computer_name.Substring (0, computer_name.Length - ".config".Length);
string arguments = string arguments =
"-t rsa " + // Crypto type "-t rsa " + // Crypto type
"-b 4096 " + // Key size "-b 4096 " + // Key size