* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / corlib / System.Security.Permissions / SiteIdentityPermission.cs
1 //
2 // System.Security.Permissions.SiteIdentityPermission.cs
3 //
4 // Author
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32
33 namespace System.Security.Permissions {
34
35 #if NET_2_0
36         [ComVisible (true)]
37 #endif
38         [Serializable]
39         public sealed class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission {
40
41                 private const int version = 1;
42
43                 private string _site;
44
45                 // Constructors
46
47                 public SiteIdentityPermission (PermissionState state) 
48                 {
49                         // false == do not allow Unrestricted for Identity Permissions
50                         CheckPermissionState (state, false);
51                 }
52
53                 public SiteIdentityPermission (string site) 
54                 {
55                         Site = site;
56                 }
57
58                 // Properties
59
60                 public string Site {
61                         get { 
62                                 if (IsEmpty ())
63                                         throw new NullReferenceException ("No site.");
64                                 return _site; 
65                         }
66                         set {
67                                 if (!IsValid (value))
68                                         throw new ArgumentException ("Invalid site.");
69                                 _site = value;
70                         }
71                 }
72
73                 // Methods
74
75                 public override IPermission Copy () 
76                 {
77                         if (IsEmpty ())
78                                 return new SiteIdentityPermission (PermissionState.None);
79                         else
80                                 return new SiteIdentityPermission (_site);
81                 }
82
83                 public override void FromXml (SecurityElement esd) 
84                 {
85                         // General validation in CodeAccessPermission
86                         CheckSecurityElement (esd, "esd", version, version);
87                         // Note: we do not (yet) care about the return value 
88                         // as we only accept version 1 (min/max values)
89
90                         string s = esd.Attribute ("Site");
91                         if (s != null)
92                                 Site = s;
93                 }
94
95                 public override IPermission Intersect (IPermission target)
96                 {
97                         SiteIdentityPermission sip = Cast (target);
98                         if ((sip == null) || (IsEmpty ()))
99                                 return null;
100
101                         if (Match (sip._site)) {
102                                 string s = ((_site.Length > sip._site.Length) ? _site : sip._site);
103                                 return new SiteIdentityPermission (s);
104                         }
105                         return null;
106                 }
107
108                 public override bool IsSubsetOf (IPermission target) 
109                 {
110                         SiteIdentityPermission sip = Cast (target);
111                         if (sip == null)
112                                 return IsEmpty ();
113                         if ((_site == null) && (sip._site == null))
114                                 return true;
115                         if ((_site == null) || (sip._site == null))
116                                 return false;
117
118                         int wildcard = sip._site.IndexOf ('*');
119                         if (wildcard == -1) {
120                                 // exact match
121                                 return (_site == sip._site);
122                         }
123                         return _site.EndsWith (sip._site.Substring (wildcard + 1));
124                 }
125
126                 public override SecurityElement ToXml ()
127                 {
128                         SecurityElement e = Element (version);
129                         if (_site != null)
130                                 e.AddAttribute ("Site", _site);
131                         return e;
132                 }
133
134                 public override IPermission Union (IPermission target) 
135                 {
136                         SiteIdentityPermission sip = Cast (target);
137                         if ((sip == null) || sip.IsEmpty ())
138                                 return Copy ();
139                         if (IsEmpty ())
140                                 return sip.Copy ();
141
142                         if (Match (sip._site)) {
143                                 string s = ((_site.Length < sip._site.Length) ? _site : sip._site);
144                                 return new SiteIdentityPermission (s);
145                         }
146 #if NET_2_0
147                         throw new ArgumentException (Locale.GetText (
148                                 "Cannot union two different sites."), "target");
149 #else
150                         return null;
151 #endif
152                 }
153
154                 // IBuiltInPermission
155                 int IBuiltInPermission.GetTokenIndex ()
156                 {
157                         return (int) BuiltInToken.SiteIdentity;
158                 }
159
160                 // helpers
161
162                 private bool IsEmpty ()
163                 {
164                         return (_site == null);
165                 }
166
167                 private SiteIdentityPermission Cast (IPermission target)
168                 {
169                         if (target == null)
170                                 return null;
171
172                         SiteIdentityPermission sip = (target as SiteIdentityPermission);
173                         if (sip == null) {
174                                 ThrowInvalidPermission (target, typeof (SiteIdentityPermission));
175                         }
176
177                         return sip;
178                 }
179
180                 private static bool[] valid = new bool [94] {
181                         /*  33 */ true,  false, true,  true,  true,  true,  true,  true,  true,  true,
182                         /*  43 */ false, false, true,  true,  false, true,  true,  true,  true,  true,
183                         /*  53 */ true,  true,  true,  true,  false, false, false, false, false, false,
184                         /*  63 */ false, true,  true,  true,  true,  true,  true,  true,  true,  true,
185                         /*  73 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
186                         /*  83 */ true,  true,  true,  true,  true,  true,  true,  true,  false, false,
187                         /*  93 */ false, true,  true,  false, true,  true,  true,  true,  true,  true,
188                         /* 103 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
189                         /* 113 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
190                         /* 123 */ true,  false, true,  true
191                 };
192
193                 private bool IsValid (string s)
194                 {
195                         if ((s == null) || (s.Length == 0))
196                                 return false;
197
198                         for (int i = 0; i < s.Length; i++) {
199                                 ushort x = (ushort) s [i];
200                                 if ((x < 33) || (x > 126)) {
201                                         return false;
202                                 }
203                                 if (x == 42) {
204                                         // special case for wildcards (*)
205                                         // must be alone or first and followed by a dot
206                                         if ((s.Length > 1) && ((s [i + 1] != '.') || (i > 0)))
207                                                 return false;
208                                 }
209                                 if (!valid [x - 33]) {
210                                         return false;
211                                 }
212                         }
213
214                         // a lone dot isn't valid
215                         if (s.Length == 1)
216                                 return (s [0] != '.');
217                         return true;
218                 }
219
220                 private bool Match (string target) 
221                 {
222                         if ((_site == null) || (target == null))
223                                 return false;
224
225                         int wcs = _site.IndexOf ('*');
226                         int wct = target.IndexOf ('*');
227
228                         if ((wcs == -1) && (wct == -1)) {
229                                 // no wildcard, this is an exact match
230                                 return (_site == target);
231                         }
232                         else if (wcs == -1) {
233                                 // only "target" has a wildcard, use it
234                                 return _site.EndsWith (target.Substring (wct + 1));
235                         }
236                         else if (wct == -1) {
237                                 // only "this" has a wildcard, use it
238                                 return target.EndsWith (_site.Substring (wcs + 1));
239                         }
240                         else {
241                                 // both have wildcards, partial match with the smallest
242                                 string s = _site.Substring (wcs + 1);
243                                 target = target.Substring (wct + 1);
244                                 if (s.Length > target.Length)
245                                         return s.EndsWith (target);
246                                 else
247                                         return target.EndsWith (s);
248                         }
249                 }
250         }
251 }