using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sphorium.WebDAV.Server.Framework;
using System.Reflection;
using Sphorium.WebDAV.Server.Framework.Classes;
using System.Web;
using System.Web.Security;
namespace Everest.CmsServices.Rfc{
public class KoobooWebDavModule : WebDAVModule
{
static List<string> WebDavMethods = new List<string>(){
"Options",
"Head",
"Delete",
"Put",
"Copy",
"Move",
"MKCol",
"PropFind",
"PropPatch",
"Lock",
"Unlock",
"VersionControl",
"Report"
};
public KoobooWebDavModule()
: base(Assembly.GetExecutingAssembly(), Authentication.Basic
)
{
this.BasicAuthorization += new BasicAuthorizationEventHandler(KoobooWebDavModule_BasicAuthorization);
}
void KoobooWebDavModule_BasicAuthorization(object sender, BasicAuthorizationArgs e)
{
e.Authorized = Membership.ValidateUser(e.UserName, e.Password);
if (e.Authorized)
{
FormsAuthentication.SetAuthCookie(e.UserName, false);
}
}
protected override void ProcessWebDavRequest(System.Web.HttpApplication _httpApp)
{
if (IsWebDavRequest(_httpApp.Request))
{
if (IsForbiddenRequest(_httpApp.Request))
{
ForbiddenRequest(_httpApp);
}
else
{
base.ProcessWebDavRequest(_httpApp);
}
}
}
private void ForbiddenRequest(HttpApplication httpApp)
{
httpApp.Response.StatusCode = 404;
httpApp.CompleteRequest();
}
private bool IsForbiddenRequest(HttpRequest request)
{
if (!string.IsNullOrEmpty(request.UserAgent) && request.UserAgent.Contains("WebDAV"))
{
if (request.RawUrl.Contains("/desktop.ini"))
{
return true;
}
if (request.RawUrl.Contains("/_svn"))
{
return true;
}
if (request.RawUrl.Contains("/.svn"))
{
return true;
}
if (request.RawUrl.Contains("/folder.jpg"))
{
return true;
}
if (request.RawUrl.Contains("/folder.gif"))
{
return true;
}
}
return false;
}
private bool IsWebDavRequest(HttpRequest request)
{
if (WebDavMethods.Contains(request.HttpMethod, StringComparer.InvariantCultureIgnoreCase))
{
return true;
}
if (request.Headers.AllKeys.Contains("Translate", StringComparer.InvariantCultureIgnoreCase))
{
return true;
}
if (request.Headers.AllKeys.Contains("Depth", StringComparer.InvariantCultureIgnoreCase))
{
return true;
}
return false;
}
protected override void context_EndRequest(object sender, EventArgs e)
{
if (IsWebDavRequest(HttpApplication.Request))
{
////redirect to login page.
if (HttpApplication.Response.StatusCode == 302)
{
DenyAccess(HttpApplication);
}
base.context_EndRequest(sender, e);
}
}
private void DenyAccess(HttpApplication httpApp)
{
httpApp.Response.Clear();
httpApp.Response.StatusCode = 0x191;
httpApp.Response.StatusDescription = "Access Denied";
httpApp.Response.Write("401 Access Denied");
httpApp.CompleteRequest();
}
}
}
|