2 // System.Security.Policy.Site.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot (spouliot@motus.com)
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Globalization;
37 using System.Security.Permissions;
38 using System.Security.Policy;
40 namespace System.Security.Policy {
43 public sealed class Site: IIdentityPermissionFactory, IBuiltInEvidence {
45 internal string origin_site;
47 public Site (string name)
50 throw new ArgumentNullException (Locale.GetText ("name is null"));
52 throw new ArgumentException (Locale.GetText ("name is not valid"));
57 public static Site CreateFromUrl (string url)
59 return new Site (url);
64 return new Site (origin_site);
67 public IPermission CreateIdentityPermission (Evidence evidence)
69 return new SiteIdentityPermission (origin_site);
72 public override bool Equals (object o)
74 if (o is System.Security.Policy.Site) {
75 return (String.Compare (((Site) o).Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
80 public override int GetHashCode ()
82 return origin_site.GetHashCode ();
85 public override string ToString ()
87 SecurityElement element = new SecurityElement (typeof (System.Security.Policy.Site).FullName);
88 element.AddAttribute ("version", "1");
89 element.AddChild (new SecurityElement ("Name", origin_site));
90 return element.ToString ();
96 get { return origin_site; }
99 // interface IBuiltInEvidence
102 int IBuiltInEvidence.GetRequiredSize (bool verbose)
108 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
114 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
121 internal static bool IsValid (string name)
123 if (name == String.Empty)
125 if ((name.Length == 1) && (name == ".")) // split would remove .
128 string [] parts = name.Split ('.');
129 for (int i=0; i < parts.Length; i++) {
130 string part = parts [i];
131 if ((i == 0) && (part == "*")) // * (only in first part)
133 foreach (char c in part) {
134 int x = Convert.ToInt32 (c);
135 bool result = ((x == 45) // -
136 || (x >= 47 && x <= 57) // /,0-9
137 || (x >= 64 && x <= 90) // @,A-Z
139 || (x >= 97 && x <= 122)); // a-z