Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WSL support via UnixSocket #14

Merged
merged 4 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions SshAgentLib/PageantAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PageantAgent : Agent
object lockObject = new object();
CygwinSocket cygwinSocket;
MsysSocket msysSocket;
UnixSocket wslSocket;
WindowsOpenSshPipe opensshPipe;

#endregion
Expand Down Expand Up @@ -286,6 +287,39 @@ public void StopMsysSocket()
msysSocket = null;
}

/// <summary>
/// Starts a wsl style socket that can be used by the ssh program
/// that comes with wsl.
/// </summary>
/// <param name="path">The path to the socket file that will be created.</param>
public void StartWslSocket(string path)
{
if (disposed) {
throw new ObjectDisposedException("PagentAgent");
}
if (wslSocket != null) {
mehrdadn marked this conversation as resolved.
Show resolved Hide resolved
return;
}
// only overwrite a file if it looks like a WslSocket file.
// TODO: Might be good to test that there are not network sockets using
// the port specified in this file.
if (File.Exists(path) && UnixSocket.TestFile(path)) {
File.Delete(path);
}
wslSocket = new UnixSocket(path);
wslSocket.ConnectionHandler = connectionHandler;
}

public void StopWslSocket()
{
if (disposed)
throw new ObjectDisposedException("PagentAgent");
if (wslSocket == null)
return;
wslSocket.Dispose();
wslSocket = null;
}

public void StartWindowsOpenSshPipe()
{
if (disposed) {
Expand Down Expand Up @@ -351,6 +385,7 @@ private void RunWindowInNewAppcontext()
// make sure socket files are cleaned up when we stop.
StopCygwinSocket();
StopMsysSocket();
StopWslSocket();
StopWindowsOpenSshPipe();

if (hwnd != IntPtr.Zero) {
Expand Down
1 change: 1 addition & 0 deletions SshAgentLib/SshAgentLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="Crypto\Ed25519PublicKeyParameter.cs" />
<Compile Include="Crypto\Ed25519Signer.cs" />
<Compile Include="Crypto\SaltParseException.cs" />
<Compile Include="UnixSocket.cs" />
<Compile Include="WindowsOpenSshPipe.cs" />
<Compile Include="MsysSocket .cs" />
<Compile Include="CygwinSocket.cs" />
Expand Down
221 changes: 221 additions & 0 deletions SshAgentLib/UnixSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
//
// UnixSocket.cs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Threading;

namespace dlech.SshAgentLib
{
public class UnixSocket : IDisposable
{
const string waitHandleNamePrefix = "unix.local_socket.secret";

static int clientCount = 0;

string path;
Socket socket;
SocketAddress sockaddr;
Guid guid;
bool disposed;
List<Socket> clientSockets = new List<Socket>();
object clientSocketsLock = new object();

public delegate void ConnectionHandlerFunc(Stream stream, Process process);
public ConnectionHandlerFunc ConnectionHandler { get; set; }

/// <summary>
/// Create new "unix domain" socket for use with Linux
/// </summary>
/// <param name="path">The name of the file to use for the socket</param>
public UnixSocket(string path)
{
this.path = path;
guid = Guid.NewGuid();
{
mehrdadn marked this conversation as resolved.
Show resolved Hide resolved
try {
socket = new Socket(AddressFamily.Unix, SocketType.Stream,
ProtocolType.Unspecified);
var endpoint = new UnixEndPoint(path);
sockaddr = endpoint.Serialize();
socket.Bind(endpoint);
var fileSecurity = File.GetAccessControl(path);
// This turns off ACL inheritance and removes all inherited rules
fileSecurity.SetAccessRuleProtection(true, false);
// We are left with no permissions at all, so we have to add them
// back for the current user
var userOnlyRule = new FileSystemAccessRule(
WindowsIdentity.GetCurrent().User,
FileSystemRights.FullControl,
AccessControlType.Allow);
fileSecurity.SetAccessRule(userOnlyRule);
File.SetAccessControl(path, fileSecurity);
socket.Listen(5);
mehrdadn marked this conversation as resolved.
Show resolved Hide resolved
var socketThread = new Thread(AcceptConnections);
socketThread.Name = "UnixSocket";
socketThread.Start();
} catch (Exception) {
if (socket != null)
socket.Close();
File.Delete(path);
throw;
}
}
}

/// <summary>
/// Tests a file to see if it looks like a Unix socket file
/// </summary>
/// <param name="path">The path to the file.</param>
/// <returns><c>true</c> if the file contents look correct</returns>
public static bool TestFile(string path)
mehrdadn marked this conversation as resolved.
Show resolved Hide resolved
{
var info = new FileInfo(path);
return info.Length == 0;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

void Dispose(bool disposing)
mehrdadn marked this conversation as resolved.
Show resolved Hide resolved
{
if (!disposed) {
disposed = true;
if (disposing) {
// Dispose managed resources
foreach (var clientSocket in clientSockets) {
clientSocket.Dispose();
}
socket.Dispose();
File.Delete(path);
}
// Dispose unmanaged resources
}
}

void AcceptConnections()
{
var buffer = new byte[16];
while (true) {
try {
BindingFlags
sbinding = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
ibinding = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
var m_Size = typeof(SocketAddress).GetField("m_Size", ibinding);
dlech marked this conversation as resolved.
Show resolved Hide resolved
var args = new object[]
{
typeof(Socket).GetField("m_Handle", ibinding).GetValue(socket),
(byte[])typeof(SocketAddress).GetField("m_Buffer", ibinding).GetValue(sockaddr),
(int)m_Size.GetValue(sockaddr)
};
var acceptedHandle = (SafeHandleMinusOneIsInvalid)typeof(Socket).Assembly.GetType("System.Net.SafeCloseSocket").GetMethod("Accept", sbinding).Invoke(null, args);
m_Size.SetValue(sockaddr, (int)args[2]);
var endpoint = new UnixEndPoint(null).Create(sockaddr);
var clientSocket = acceptedHandle.IsInvalid ? null : (Socket)typeof(Socket).GetMethod("CreateAcceptSocket", ibinding).Invoke(socket, new object[] { acceptedHandle, endpoint, false });
if (clientSocket == null) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); }
var clientThread = new Thread(() => {
try {
using (var stream = new NetworkStream(clientSocket)) {
Process proc = null;
if (ConnectionHandler != null) {
ConnectionHandler(stream, proc);
}
}
} catch {
// can throw if remote closes the connection at a bad time
} finally {
lock (clientSocketsLock) {
clientSockets.Remove(clientSocket);
}
}
});
lock (clientSocketsLock) {
clientSockets.Add(clientSocket);
}
clientThread.Name = string.Format("UnixClient{0}", clientCount++);
clientThread.Start();
} catch (Exception ex) {
Debug.Assert(disposed, ex.ToString());
break;
}
}
}
}

[Serializable]
public class UnixEndPoint : EndPoint
{
public string Filename { get; private set; }

public UnixEndPoint(string path) : base()
{
this.Filename = path;
}

public override AddressFamily AddressFamily { get { return AddressFamily.Unix; } }

public override EndPoint Create(SocketAddress socketAddress)
{
int size = socketAddress.Size - 2;
var bytes = new byte[size];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = socketAddress[i + 2];
if (i > 0 && bytes[i] == 0)
{
size = i;
break;
}
}
return new UnixEndPoint(Encoding.UTF8.GetString(bytes, 0, size));
}
public override SocketAddress Serialize()
{
var bytes = Encoding.UTF8.GetBytes(this.Filename);
var maxLen = 108;
if (bytes.Length > maxLen) {
throw new PathTooLongException(string.Format("Path ({0} bytes) was too long for UNIX-domain socket (max {1} bytes)", bytes.Length, maxLen));
}
var addr = new SocketAddress(AddressFamily.Unix, sizeof(short) + maxLen);
for (int i = 0; i < bytes.Length && i < maxLen; i++)
{
addr[2 + i] = bytes[i];
}
if (bytes.Length < maxLen) {
addr[sizeof(short) + bytes.Length] = 0;
}
return addr;
}
}
}