-
-
Notifications
You must be signed in to change notification settings - Fork 278
Add named pipe (RPC/NP) transport support to SamServer #223
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
Merged
+176
−6
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
| using Windows.Win32.NetworkManagement.WNet; | ||
|
|
||
| namespace DSInternals.SAM.Interop; | ||
|
|
||
| /// <summary> | ||
| /// Specifies the type of disconnection to perform when calling WNetCancelConnection2. | ||
| /// </summary> | ||
| /// <see>https://learn.microsoft.com/windows/win32/api/winnetwk/nf-winnetwk-wnetcancelconnection2w</see> | ||
| [Flags] | ||
| internal enum NetCancelOptions : uint | ||
| { | ||
| /// <summary> | ||
| /// The system does not update the user profile with information about the disconnection. | ||
| /// </summary> | ||
| NoUpdate = 0U, | ||
|
|
||
| /// <summary> | ||
| /// The system updates the user profile with the information that the connection is no longer a persistent one. | ||
| /// </summary> | ||
| UpdateProfile = (uint)NET_CONNECT_FLAGS.CONNECT_UPDATE_PROFILE | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| using System.Net; | ||
| using DSInternals.Common; | ||
| using DSInternals.Common.Interop; | ||
| using Windows.Win32.NetworkManagement.WNet; | ||
|
|
||
| namespace DSInternals.SAM.Interop; | ||
|
|
||
| /// <summary> | ||
| /// Represents an authenticated SMB connection to a remote server's IPC$ share, | ||
| /// to be used by SAM RPC over named pipes (ncacn_np). | ||
| /// </summary> | ||
| internal sealed class NamedPipeConnection : IDisposable | ||
| { | ||
| private readonly string _shareName; | ||
|
|
||
| internal NamedPipeConnection(string server, NetworkCredential? credential) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(server); | ||
|
|
||
| _shareName = $"\\\\{server}\\IPC$"; | ||
|
|
||
| // Disconnect from the IPC share first in case of a preexisting connection. Ignore any errors. | ||
| Disconnect(); | ||
|
|
||
| // Connect using provided credentials | ||
| Win32ErrorCode result = NativeMethods.WNetAddConnection2( | ||
| _shareName, | ||
| credential, | ||
| NET_CONNECT_FLAGS.CONNECT_TEMPORARY | ||
| ); | ||
|
|
||
| Validator.AssertSuccess(result); | ||
| } | ||
|
|
||
| private void Disconnect() | ||
| { | ||
| // Ignore errors during disconnect | ||
| NativeMethods.WNetCancelConnection2(_shareName, NetCancelOptions.NoUpdate, force: true); | ||
| } | ||
|
|
||
MichaelGrafnetter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public void Dispose() | ||
| { | ||
| Disconnect(); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| ~NamedPipeConnection() | ||
| { | ||
| Disconnect(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System.Net; | ||
| using System.Runtime.InteropServices; | ||
| using DSInternals.Common; | ||
| using DSInternals.Common.Interop; | ||
| using Windows.Win32.Foundation; | ||
| using Windows.Win32.NetworkManagement.WNet; | ||
|
|
||
| namespace DSInternals.SAM.Interop; | ||
|
|
||
| /// <summary> | ||
| /// Contains P/Invoke signatures for mpr.dll functions. | ||
| /// </summary> | ||
| internal static partial class NativeMethods | ||
| { | ||
| private const string Mpr = "mpr.dll"; | ||
|
|
||
| /// <summary> | ||
| /// Makes a connection to a network resource using the specified credentials. | ||
| /// </summary> | ||
| /// <param name="shareName">The remote network resource to connect to (e.g., \\server\IPC$).</param> | ||
| /// <param name="credential">The credentials to use for the connection, or <c>null</c> to use the default credentials.</param> | ||
| /// <param name="flags">A set of connection options.</param> | ||
| internal static unsafe Win32ErrorCode WNetAddConnection2(string shareName, NetworkCredential? credential, NET_CONNECT_FLAGS flags) | ||
| { | ||
| fixed (char* remoteNamePtr = shareName) | ||
| { | ||
| NETRESOURCEW resource = new() | ||
| { | ||
| dwScope = NET_RESOURCE_SCOPE.RESOURCE_GLOBALNET, | ||
| dwType = NET_RESOURCE_TYPE.RESOURCETYPE_ANY, | ||
| lpRemoteName = new PWSTR(remoteNamePtr) | ||
| }; | ||
|
|
||
| string? userName = credential?.GetLogonName(); | ||
| IntPtr passwordPtr = credential != null | ||
| ? Marshal.SecureStringToGlobalAllocUnicode(credential.SecurePassword) | ||
| : IntPtr.Zero; | ||
|
|
||
| try | ||
| { | ||
| return WNetAddConnection2(resource, passwordPtr, userName, flags); | ||
| } | ||
| finally | ||
| { | ||
| if (passwordPtr != IntPtr.Zero) | ||
| { | ||
| Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <see>https://learn.microsoft.com/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2w</see> | ||
| [DllImport(Mpr, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "WNetAddConnection2W")] | ||
| private static extern Win32ErrorCode WNetAddConnection2(in NETRESOURCEW netResource, IntPtr password, string? userName, NET_CONNECT_FLAGS flags); | ||
|
|
||
| /// <summary> | ||
| /// The WNetCancelConnection2 function cancels an existing network connection. You can also call the function to remove remembered network connections that are not currently connected. | ||
| /// </summary> | ||
| /// <param name="name">The name of either the redirected local device or the remote network resource to disconnect from.</param> | ||
| /// <param name="flags">Connection type.</param> | ||
| /// <param name="force">Specifies whether the disconnection should occur if there are open files or jobs on the connection.</param> | ||
| /// <see>https://learn.microsoft.com/windows/win32/api/winnetwk/nf-winnetwk-wnetcancelconnection2w</see> | ||
| [DllImport(Mpr, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "WNetCancelConnection2W")] | ||
| internal static extern Win32ErrorCode WNetCancelConnection2(string name, NetCancelOptions flags, [MarshalAs(UnmanagedType.Bool)] bool force); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.