//
// Author
// Duncan Mak (duncan@ximian.com)
+// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2003 Ximian, Inc (http://www.ximian.com)
+// (C) 2004 Motus Technologies Inc. (http://www.motus.com)
//
using System;
namespace System.Security.Policy {
[Serializable]
- public sealed class Url: IIdentityPermissionFactory {
+ public sealed class Url: IIdentityPermissionFactory, IBuiltInEvidence {
string origin_url;
- SecurityElement element;
public Url (string name)
{
- if (name == null)
- throw new ArgumentNullException (Locale.GetText ("name is null"));
-
- origin_url = name;
- element = new SecurityElement (
- typeof (System.Security.Policy.Url).FullName);
-
- element.AddAttribute ("version", "1");
- element.AddChild (new SecurityElement ("Url", name));
- }
-
- private Url (string name, SecurityElement security_element)
- {
- origin_url = name;
- element = security_element;
+ origin_url = Prepare (name);
}
public object Copy ()
{
- return new Url (origin_url, element);
+ return new Url (origin_url);
}
public IPermission CreateIdentityPermission (Evidence evidence)
public override bool Equals (object o)
{
- return (o is System.Security.Policy.Url && ((Url) o).Value == Value);
+ if (o is System.Security.Policy.Url)
+ return (String.Compare (((Url) o).Value, Value, true, CultureInfo.InvariantCulture) == 0);
+ return false;
}
public override int GetHashCode ()
public override string ToString ()
{
- return element.ToString ();
+ SecurityElement element = new SecurityElement (typeof (System.Security.Policy.Url).FullName);
+ element.AddAttribute ("version", "1");
+ element.AddChild (new SecurityElement ("Url", origin_url));
+ return element.ToString ();
}
public string Value {
- get {
- return origin_url;
- }
+ get { return origin_url; }
}
+
+ // interface IBuiltInEvidence
+
+ [MonoTODO]
+ int IBuiltInEvidence.GetRequiredSize (bool verbose)
+ {
+ return 0;
+ }
+
+ [MonoTODO]
+ int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
+ {
+ return 0;
+ }
+
+ [MonoTODO]
+ int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
+ {
+ return 0;
+ }
+
+ // internal
+
+ [MonoTODO ("missing site validation")]
+ internal static string Prepare (string url)
+ {
+ if (url == null)
+ throw new ArgumentNullException ("Url");
+ if (url == String.Empty)
+ throw new FormatException (Locale.GetText ("Invalid (empty) Url"));
+
+ // is a protocol specified
+ int protocolPos = url.IndexOf ("://");
+ if (protocolPos == -1)
+ return "file://" + url.ToUpper ();
+
+ if (url.StartsWith ("file://"))
+ return "file://" + url.Substring (7).ToUpper ();
+
+ // add a trailing slash if none (lonely one) is present
+ if (url.LastIndexOf ("/") == protocolPos + 2)
+ return url + "/";
+ else
+ return url;
+ }
+
+ internal static bool Compare (string mask, string url)
+ {
+ int wildcard = mask.LastIndexOf ("*");
+ if (wildcard > 0) {
+ // partial match with a wildcard at the end
+ return (String.Compare (mask, 0, url, 0, wildcard, true, CultureInfo.InvariantCulture) == 0);
+ }
+ else {
+ // exact match
+ return (String.Compare (mask, url, true, CultureInfo.InvariantCulture) == 0);
+ }
+ }
}
}
//
// System.Security.Policy.UrlMembershipCondition.cs
//
-// Author:
-// Duncan Mak (duncan@ximian.com)
+// Authors:
+// Duncan Mak (duncan@ximian.com)
+// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2003, Ximian Inc.
+// (C) 2004 Motus Technologies Inc. (http://www.motus.com)
//
using System;
namespace System.Security.Policy {
+ [Serializable]
public sealed class UrlMembershipCondition
: IMembershipCondition, ISecurityEncodable, ISecurityPolicyEncodable, IConstantMembershipCondition
{
public UrlMembershipCondition (string url)
{
- if (url == null)
- throw new ArgumentNullException (
- Locale.GetText ("The argument is null."));
-
- this.url = url;
+ this.url = System.Security.Policy.Url.Prepare (url);
}
public string Url {
get { return url; }
+ set { url = System.Security.Policy.Url.Prepare (value); }
}
- [MonoTODO]
public bool Check (Evidence evidence)
{
+ if (evidence == null)
+ return false;
+
+ foreach (object o in evidence) {
+ Url u = (o as Url);
+ if (u != null) {
+ // note: there shouldn't be more than one Url evidence
+ if (System.Security.Policy.Url.Compare (url, u.Value))
+ return true;
+ }
+ }
return false;
}
public override bool Equals (Object o)
{
- if (o is UrlMembershipCondition == false)
- return false;
- else
- return ((UrlMembershipCondition) o).Url == url;
+ if (o is UrlMembershipCondition) {
+ return System.Security.Policy.Url.Compare (url, ((UrlMembershipCondition) o).Url);
+ }
+ return false;
}
public void FromXml (SecurityElement element)
public void FromXml (SecurityElement element, PolicyLevel level)
{
if (element == null)
- throw new ArgumentNullException (
- Locale.GetText ("The argument is null."));
+ throw new ArgumentNullException ("element");
+
+ if (element.Tag != "IMembershipCondition")
+ throw new ArgumentException (
+ Locale.GetText ("Invalid tag - expected IMembershipCondition"));
if (element.Attribute ("class") != GetType ().AssemblyQualifiedName)
throw new ArgumentException (
- Locale.GetText ("The argument is invalid."));
+ Locale.GetText ("Invalid class attribute"));
if (element.Attribute ("version") != "1")
throw new ArgumentException (
- Locale.GetText ("The argument is invalid."));
+ Locale.GetText ("Invalid version"));
url = element.Attribute ("Url");
}
public SecurityElement ToXml (PolicyLevel level)
{
SecurityElement element = new SecurityElement ("IMembershipCondition");
- element.AddAttribute ("version", "1");
-
+ element.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
+ element.AddAttribute ("version", "1");
element.AddAttribute ("Url", url);
-
return element;
}
}