System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / corlib / System.Security.Policy / Site.cs
1 //
2 // System.Security.Policy.Site.cs
3 //
4 // Authors
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
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)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
35
36 using Mono.Security;
37
38 namespace System.Security.Policy {
39
40         [Serializable]
41         [ComVisible (true)]
42         public sealed class Site: IIdentityPermissionFactory, IBuiltInEvidence {
43
44                 internal string origin_site;
45
46                 public Site (string name)
47                 {
48                         if (name == null)
49                                 throw new ArgumentNullException ("url");
50                         if (!IsValid (name))
51                                 throw new ArgumentException (Locale.GetText ("name is not valid"));
52                         
53                         origin_site = name;
54                 }
55
56                 public static Site CreateFromUrl (string url)
57                 {
58                         if (url == null)
59                                 throw new ArgumentNullException ("url");
60                         if (url.Length == 0)
61                                 throw new FormatException (Locale.GetText ("Empty URL."));
62
63                         string site = UrlToSite (url);
64                         if (site == null) {
65                                 string msg = String.Format (Locale.GetText ("Invalid URL '{0}'."), url);
66                                 throw new ArgumentException (msg, "url");
67                         }
68
69                         return new Site (site);
70                 }
71
72                 public object Copy ()
73                 {
74                         return new Site (origin_site);
75                 }
76
77                 public IPermission CreateIdentityPermission (Evidence evidence)
78                 {
79                         return new SiteIdentityPermission (origin_site);
80                 }
81
82                 public override bool Equals (object o)
83                 {
84                         Site s = (o as System.Security.Policy.Site);
85                         if (s == null)
86                                 return false;
87                         return (String.Compare (s.Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
88                 }
89
90                 public override int GetHashCode ()
91                 {
92                         return origin_site.GetHashCode ();
93                 }
94
95                 public override string ToString ()
96                 {
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 ();
101                 }
102
103                 // properties
104
105                 public string Name {
106                         get { return origin_site; }
107                 }
108
109                 // interface IBuiltInEvidence
110
111                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
112                 {
113                         return (verbose ? 3 : 1) + origin_site.Length;
114                 }
115
116                 [MonoTODO ("IBuiltInEvidence")]
117                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
118                 {
119                         return 0;
120                 }
121
122                 [MonoTODO ("IBuiltInEvidence")]
123                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
124                 {
125                         return 0;
126                 }
127
128                 // internals
129
130                 internal static bool IsValid (string name)
131                 {
132                         if (name == String.Empty)
133                                 return false;
134                         if ((name.Length == 1) && (name == "."))                // split would remove .
135                                 return false;
136
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)
141                                         continue;
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));     // }~
151                                         if (!result)
152                                                 return false;
153                                 }
154                         }
155                         return true;
156                 }
157
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)
162                 {
163                         if (url == null)
164                                 return null;
165
166                         Uri uri = new Uri (url);
167                         if (uri.Scheme == Uri.UriSchemeFile)
168                                 return null;
169                         string site = uri.Host;
170                         return IsValid (site) ? site : null;
171                 }
172         }
173 }