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 {
42 public sealed class Site: IIdentityPermissionFactory, IBuiltInEvidence {
44 internal string origin_site;
46 public Site (string name)
49 throw new ArgumentNullException ("url");
51 throw new ArgumentException (Locale.GetText ("name is not valid"));
56 public static Site CreateFromUrl (string url)
59 throw new ArgumentNullException ("url");
61 throw new FormatException (Locale.GetText ("Empty URL."));
63 string site = UrlToSite (url);
65 string msg = String.Format (Locale.GetText ("Invalid URL '{0}'."), url);
66 throw new ArgumentException (msg, "url");
69 return new Site (site);
74 return new Site (origin_site);
77 public IPermission CreateIdentityPermission (Evidence evidence)
79 return new SiteIdentityPermission (origin_site);
82 public override bool Equals (object o)
84 Site s = (o as System.Security.Policy.Site);
87 return (String.Compare (s.Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
90 public override int GetHashCode ()
92 return origin_site.GetHashCode ();
95 public override string ToString ()
97 SecurityElement element = new SecurityElement ("System.Security.Policy.Site");
98 element.AddAttribute ("version", "1");
99 element.AddChild (new SecurityElement ("Name", origin_site));
100 return element.ToString ();
106 get { return origin_site; }
109 // interface IBuiltInEvidence
111 int IBuiltInEvidence.GetRequiredSize (bool verbose)
113 return (verbose ? 3 : 1) + origin_site.Length;
116 [MonoTODO ("IBuiltInEvidence")]
117 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
122 [MonoTODO ("IBuiltInEvidence")]
123 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
130 internal static bool IsValid (string name)
132 if (name == String.Empty)
134 if ((name.Length == 1) && (name == ".")) // split would remove .
137 string [] parts = name.Split ('.');
138 for (int i=0; i < parts.Length; i++) {
139 string part = parts [i];
140 if ((i == 0) && (part == "*")) // * (only in first part)
142 foreach (char c in part) {
143 int x = Convert.ToInt32 (c);
144 bool result = ((x == 33) || (x == 45) // !-
145 || (x >= 35 && x <= 41) // #$%&'()
146 || (x >= 48 && x <= 57) // 0-9
147 || (x >= 64 && x <= 90) // @,A-Z
148 || (x >= 94 && x <= 95) // ^_
149 || (x >= 97 && x <= 123) // a-z{
150 || (x >= 125 && x <= 126)); // }~
158 // no exception - we return null if a site couldn't be created
159 // this is useful for creating the default evidence as the majority of URL will be local (file://)
160 // and throw an unrequired exception
161 internal static string UrlToSite (string url)
166 Uri uri = new Uri (url);
167 if (uri.Scheme == Uri.UriSchemeFile)
169 string site = uri.Host;
170 return IsValid (site) ? site : null;