2 // System.Security.Policy.Site.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
38 namespace System.Security.Policy {
44 public sealed class Site: IIdentityPermissionFactory, IBuiltInEvidence {
46 internal string origin_site;
48 public Site (string name)
51 throw new ArgumentNullException ("url");
53 throw new ArgumentException (Locale.GetText ("name is not valid"));
58 public static Site CreateFromUrl (string url)
61 throw new ArgumentNullException ("url");
63 throw new FormatException (Locale.GetText ("Empty URL."));
65 string site = UrlToSite (url);
67 string msg = String.Format (Locale.GetText ("Invalid URL '{0}'."), url);
68 throw new ArgumentException (msg, "url");
71 return new Site (site);
76 return new Site (origin_site);
79 public IPermission CreateIdentityPermission (Evidence evidence)
81 return new SiteIdentityPermission (origin_site);
84 public override bool Equals (object o)
86 Site s = (o as System.Security.Policy.Site);
89 return (String.Compare (s.Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
92 public override int GetHashCode ()
94 return origin_site.GetHashCode ();
97 public override string ToString ()
99 SecurityElement element = new SecurityElement ("System.Security.Policy.Site");
100 element.AddAttribute ("version", "1");
101 element.AddChild (new SecurityElement ("Name", origin_site));
102 return element.ToString ();
108 get { return origin_site; }
111 // interface IBuiltInEvidence
113 int IBuiltInEvidence.GetRequiredSize (bool verbose)
115 return (verbose ? 3 : 1) + origin_site.Length;
118 [MonoTODO ("IBuiltInEvidence")]
119 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
124 [MonoTODO ("IBuiltInEvidence")]
125 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
132 internal static bool IsValid (string name)
134 if (name == String.Empty)
136 if ((name.Length == 1) && (name == ".")) // split would remove .
139 string [] parts = name.Split ('.');
140 for (int i=0; i < parts.Length; i++) {
141 string part = parts [i];
142 if ((i == 0) && (part == "*")) // * (only in first part)
144 foreach (char c in part) {
145 int x = Convert.ToInt32 (c);
147 bool result = ((x == 33) || (x == 45) // !-
148 || (x >= 35 && x <= 41) // #$%&'()
149 || (x >= 48 && x <= 57) // 0-9
150 || (x >= 64 && x <= 90) // @,A-Z
151 || (x >= 94 && x <= 95) // ^_
152 || (x >= 97 && x <= 123) // a-z{
153 || (x >= 125 && x <= 126)); // }~
155 bool result = ((x == 45) // -
156 || (x >= 47 && x <= 57) // /,0-9
157 || (x >= 64 && x <= 90) // @,A-Z
159 || (x >= 97 && x <= 122)); // a-z
168 // no exception - we return null if a site couldn't be created
169 // this is useful for creating the default evidence as the majority of URL will be local (file://)
170 // and throw an unrequired exception
171 internal static string UrlToSite (string url)
176 Uri uri = new Uri (url);
178 if (uri.Scheme == Uri.UriSchemeFile)
180 string site = uri.Host;
182 string site = uri.Host;
184 site = uri.AbsoluteUri.ToUpper (); // strange but true
186 return IsValid (site) ? site : null;