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

New functionality (Locks/ Authenticate Modes) #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
74 changes: 74 additions & 0 deletions Adapters/AuthenticationTypes/HttpListenerAnyonymousAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Net;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Threading;
using Microsoft.Win32.SafeHandles;

namespace WebDAVSharp.Server.Adapters.AuthenticationTypes
{
class HttpListenerAnyonymousAdapter : WebDavDisposableBase, IHttpListener, IAdapter<HttpListener>
{
public HttpListenerAnyonymousAdapter()
{
AdaptedInstance = new HttpListener
{
AuthenticationSchemes = AuthenticationSchemes.Anonymous,
UnsafeConnectionNtlmAuthentication = false
};
}

protected override void Dispose(bool disposing)
{
if (AdaptedInstance.IsListening)
AdaptedInstance.Close();
}

public HttpListener AdaptedInstance
{
get;
private set;
}

public IHttpListenerContext GetContext(EventWaitHandle abortEvent)
{
if (abortEvent == null)
throw new ArgumentNullException("abortEvent");

IAsyncResult ar = AdaptedInstance.BeginGetContext(null, null);
int index = WaitHandle.WaitAny(new[] { abortEvent, ar.AsyncWaitHandle });
if (index != 1)
return null;
HttpListenerContext context = AdaptedInstance.EndGetContext(ar);
return new HttpListenerContextAdapter(context);
}

public HttpListenerPrefixCollection Prefixes
{
get
{
return AdaptedInstance.Prefixes;
}
}

public void Start()
{
AdaptedInstance.Start();
}

public void Stop()
{
AdaptedInstance.Stop();
}

public IIdentity GetIdentity(IHttpListenerContext context)
{
return WindowsIdentity.GetCurrent();
}



}
}
124 changes: 124 additions & 0 deletions Adapters/AuthenticationTypes/HttpListenerBasicAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Net;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Threading;
using Microsoft.Win32.SafeHandles;

namespace WebDAVSharp.Server.Adapters.AuthenticationTypes
{
class HttpListenerBasicAdapter : WebDavDisposableBase, IHttpListener, IAdapter<HttpListener>
{
#region Imports
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

#endregion

public HttpListenerBasicAdapter()
{
AdaptedInstance = new HttpListener
{
AuthenticationSchemes = AuthenticationSchemes.Basic,
UnsafeConnectionNtlmAuthentication = false
};
}

protected override void Dispose(bool disposing)
{
if (AdaptedInstance.IsListening)
AdaptedInstance.Close();
}

public HttpListener AdaptedInstance
{
get;
private set;
}

public IHttpListenerContext GetContext(EventWaitHandle abortEvent)
{
if (abortEvent == null)
throw new ArgumentNullException("abortEvent");

IAsyncResult ar = AdaptedInstance.BeginGetContext(null, null);
int index = WaitHandle.WaitAny(new[] { abortEvent, ar.AsyncWaitHandle });
if (index != 1)
return null;
HttpListenerContext context = AdaptedInstance.EndGetContext(ar);
return new HttpListenerContextAdapter(context);
}

public HttpListenerPrefixCollection Prefixes
{
get
{
return AdaptedInstance.Prefixes;
}
}

public void Start()
{
AdaptedInstance.Start();
}

public void Stop()
{
AdaptedInstance.Stop();
}

public IIdentity GetIdentity(IHttpListenerContext context)
{
HttpListenerBasicIdentity ident = (HttpListenerBasicIdentity)context.AdaptedInstance.User.Identity;
string domain = ident.Name.Split('\\')[0];
string username = ident.Name.Split('\\')[1];
var token = GetToken(domain, username, ident.Password);
return new WindowsIdentity(token.DangerousGetHandle());
}



internal static SafeTokenHandle GetToken(string domainName,
string userName, string password)
{
SafeTokenHandle safeTokenHandle;

const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;

// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);

if (returnValue) return safeTokenHandle;
int ret = Marshal.GetLastWin32Error();
throw new System.ComponentModel.Win32Exception(ret);
}

public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private SafeTokenHandle()
: base(true)
{
}

[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);

protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
using System;
using System.Net;
using System.Security.Principal;
using System.Threading;

namespace WebDAVSharp.Server.Adapters
namespace WebDAVSharp.Server.Adapters.AuthenticationTypes
{
/// <summary>
/// This
/// <see cref="IHttpListener" /> implementation wraps around a
/// <see cref="HttpListener" /> instance.
/// </summary>
internal sealed class HttpListenerAdapter : WebDavDisposableBase, IHttpListener, IAdapter<HttpListener>
internal sealed class HttpListenerNegotiateAdapter : WebDavDisposableBase, IHttpListener, IAdapter<HttpListener>
{
#region Private Variables

private readonly HttpListener _listener;

#endregion

#region Properties
/// <summary>
/// Initializes a new instance of the <see cref="HttpListenerAdapter" /> class.
/// Gets the internal instance that was adapted for WebDAV#.
/// </summary>
internal HttpListenerAdapter()
/// <value>
/// The adapted instance.
/// </value>
public HttpListener AdaptedInstance
{
get
{
return _listener;
}
}

/// <summary>
/// Gets the Uniform Resource Identifier (
/// <see cref="Uri" />) prefixes handled by the
/// adapted
/// <see cref="HttpListener" /> object.
/// </summary>
public HttpListenerPrefixCollection Prefixes
{
get
{
return _listener.Prefixes;
}
}
#endregion

#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="HttpListenerNegotiateAdapter" /> class.
/// </summary>
internal HttpListenerNegotiateAdapter()
{
_listener = new HttpListener
{
AuthenticationSchemes = AuthenticationSchemes.Negotiate,
UnsafeConnectionNtlmAuthentication = false
};
}
#endregion

#region Function Overrides
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
Expand All @@ -35,6 +73,10 @@ protected override void Dispose(bool disposing)
_listener.Close();
}

#endregion

#region Public Functions

/// <summary>
/// Waits for a request to come in to the web server and returns a
/// <see cref="IHttpListenerContext" /> adapter around it.
Expand Down Expand Up @@ -63,35 +105,7 @@ public IHttpListenerContext GetContext(EventWaitHandle abortEvent)
HttpListenerContext context = _listener.EndGetContext(ar);
return new HttpListenerContextAdapter(context);
}

/// <summary>
/// Gets the internal instance that was adapted for WebDAV#.
/// </summary>
/// <value>
/// The adapted instance.
/// </value>
public HttpListener AdaptedInstance
{
get
{
return _listener;
}
}

/// <summary>
/// Gets the Uniform Resource Identifier (
/// <see cref="Uri" />) prefixes handled by the
/// adapted
/// <see cref="HttpListener" /> object.
/// </summary>
public HttpListenerPrefixCollection Prefixes
{
get
{
return _listener.Prefixes;
}
}


/// <summary>
/// Allows the adapted <see cref="HttpListener" /> to receive incoming requests.
/// </summary>
Expand All @@ -107,5 +121,12 @@ public void Stop()
{
_listener.Stop();
}

public IIdentity GetIdentity(IHttpListenerContext context)
{
return context.AdaptedInstance.User.Identity;
}

#endregion
}
}
14 changes: 13 additions & 1 deletion Adapters/HttpListenerContextAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ namespace WebDAVSharp.Server.Adapters
/// <see cref="IHttpListenerContext" /> implementation wraps around a
/// <see cref="HttpListenerContext" /> instance.
/// </summary>
internal sealed class HttpListenerContextAdapter : IHttpListenerContext, IAdapter<HttpListenerContext>
public sealed class HttpListenerContextAdapter : IHttpListenerContext, IAdapter<HttpListenerContext>
{
#region Private Variables

private readonly HttpListenerContext _context;
private readonly HttpListenerRequestAdapter _request;
private readonly HttpListenerResponseAdapter _response;

#endregion

#region Public Functions

/// <summary>
/// Initializes a new instance of the <see cref="HttpListenerContextAdapter" /> class.
/// </summary>
Expand All @@ -30,6 +36,10 @@ public HttpListenerContextAdapter(HttpListenerContext context)
_response = new HttpListenerResponseAdapter(context.Response);
}

#endregion

#region Properties

/// <summary>
/// Gets the internal instance that was adapted for WebDAV#.
/// </summary>
Expand Down Expand Up @@ -65,5 +75,7 @@ public IHttpListenerResponse Response
return _response;
}
}

#endregion
}
}
Loading