2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 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
32 namespace System.Security.Permissions {
33
34         [Serializable]
35         public sealed class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission {
36
37                 private const int version = 1;
38
39                 private string _site;
40
41                 // Constructors
42
43                 public SiteIdentityPermission (PermissionState state) 
44                 {
45                         // false == do not allow Unrestricted for Identity Permissions
46                         CheckPermissionState (state, false);
47                 }
48
49                 public SiteIdentityPermission (string site) 
50                 {
51                         Site = site;
52                 }
53
54                 // Properties
55
56                 public string Site {
57                         get { 
58                                 if (IsEmpty ())
59                                         throw new NullReferenceException ("No site.");
60                                 return _site; 
61                         }
62                         set {
63                                 if (!IsValid (value))
64                                         throw new ArgumentException ("Invalid site.");
65                                 _site = value;
66                         }
67                 }
68
69                 // Methods
70
71                 public override IPermission Copy () 
72                 {
73                         if (IsEmpty ())
74                                 return new SiteIdentityPermission (PermissionState.None);
75                         else
76                                 return new SiteIdentityPermission (_site);
77                 }
78
79                 public override void FromXml (SecurityElement esd) 
80                 {
81                         // General validation in CodeAccessPermission
82                         CheckSecurityElement (esd, "esd", version, version);
83                         // Note: we do not (yet) care about the return value 
84                         // as we only accept version 1 (min/max values)
85
86                         string s = esd.Attribute ("Site");
87                         if (s != null)
88                                 Site = s;
89                 }
90
91                 public override IPermission Intersect (IPermission target)
92                 {
93                         SiteIdentityPermission sip = Cast (target);
94                         if ((sip == null) || (IsEmpty ()))
95                                 return null;
96
97                         if (Match (sip._site)) {
98                                 string s = ((_site.Length > sip._site.Length) ? _site : sip._site);
99                                 return new SiteIdentityPermission (s);
100                         }
101                         return null;
102                 }
103
104                 public override bool IsSubsetOf (IPermission target) 
105                 {
106                         SiteIdentityPermission sip = Cast (target);
107                         if (sip == null)
108                                 return IsEmpty ();
109                         if ((_site == null) && (sip._site == null))
110                                 return true;
111                         if ((_site == null) || (sip._site == null))
112                                 return false;
113
114                         int wildcard = sip._site.IndexOf ('*');
115                         if (wildcard == -1) {
116                                 // exact match
117                                 return (_site == sip._site);
118                         }
119                         return _site.EndsWith (sip._site.Substring (wildcard + 1));
120                 }
121
122                 public override SecurityElement ToXml ()
123                 {
124                         SecurityElement e = Element (version);
125                         if (_site != null)
126                                 e.AddAttribute ("Site", _site);
127                         return e;
128                 }
129
130                 public override IPermission Union (IPermission target) 
131                 {
132                         SiteIdentityPermission sip = Cast (target);
133                         if ((sip == null) || sip.IsEmpty ())
134                                 return Copy ();
135                         if (IsEmpty ())
136                                 return sip.Copy ();
137
138                         if (Match (sip._site)) {
139                                 string s = ((_site.Length < sip._site.Length) ? _site : sip._site);
140                                 return new SiteIdentityPermission (s);
141                         }
142 #if NET_2_0
143                         throw new ArgumentException (Locale.GetText (
144                                 "Cannot union two different sites."), "target");
145 #else
146                         return null;
147 #endif
148                 }
149
150                 // IBuiltInPermission
151                 int IBuiltInPermission.GetTokenIndex ()
152                 {
153                         return (int) BuiltInToken.SiteIdentity;
154                 }
155
156                 // helpers
157
158                 private bool IsEmpty ()
159                 {
160                         return (_site == null);
161                 }
162
163                 private SiteIdentityPermission Cast (IPermission target)
164                 {
165                         if (target == null)
166                                 return null;
167
168                         SiteIdentityPermission sip = (target as SiteIdentityPermission);
169                         if (sip == null) {
170                                 ThrowInvalidPermission (target, typeof (SiteIdentityPermission));
171                         }
172
173                         return sip;
174                 }
175
176                 private static bool[] valid = new bool [94] {
177                         /*  33 */ true,  false, true,  true,  true,  true,  true,  true,  true,  true,
178                         /*  43 */ false, false, true,  true,  false, true,  true,  true,  true,  true,
179                         /*  53 */ true,  true,  true,  true,  false, false, false, false, false, false,
180                         /*  63 */ false, true,  true,  true,  true,  true,  true,  true,  true,  true,
181                         /*  73 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
182                         /*  83 */ true,  true,  true,  true,  true,  true,  true,  true,  false, false,
183                         /*  93 */ false, true,  true,  false, true,  true,  true,  true,  true,  true,
184                         /* 103 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
185                         /* 113 */ true,  true,  true,  true,  true,  true,  true,  true,  true,  true,
186                         /* 123 */ true,  false, true,  true
187                 };
188
189                 private bool IsValid (string s)
190                 {
191                         if ((s == null) || (s.Length == 0))
192                                 return false;
193
194                         for (int i = 0; i < s.Length; i++) {
195                                 ushort x = (ushort) s [i];
196                                 if ((x < 33) || (x > 126)) {
197                                         return false;
198                                 }
199                                 if (x == 42) {
200                                         // special case for wildcards (*)
201                                         // must be alone or first and followed by a dot
202                                         if ((s.Length > 1) && ((s [i + 1] != '.') || (i > 0)))
203                                                 return false;
204                                 }
205                                 if (!valid [x - 33]) {
206                                         return false;
207                                 }
208                         }
209
210                         // a lone dot isn't valid
211                         if (s.Length == 1)
212                                 return (s [0] != '.');
213                         return true;
214                 }
215
216                 private bool Match (string target) 
217                 {
218                         if ((_site == null) || (target == null))
219                                 return false;
220
221                         int wcs = _site.IndexOf ('*');
222                         int wct = target.IndexOf ('*');
223
224                         if ((wcs == -1) && (wct == -1)) {
225                                 // no wildcard, this is an exact match
226                                 return (_site == target);
227                         }
228                         else if (wcs == -1) {
229                                 // only "target" has a wildcard, use it
230                                 return _site.EndsWith (target.Substring (wct + 1));
231                         }
232                         else if (wct == -1) {
233                                 // only "this" has a wildcard, use it
234                                 return target.EndsWith (_site.Substring (wcs + 1));
235                         }
236                         else {
237                                 // both have wildcards, partial match with the smallest
238                                 string s = _site.Substring (wcs + 1);
239                                 target = target.Substring (wct + 1);
240                                 if (s.Length > target.Length)
241                                         return s.EndsWith (target);
242                                 else
243                                         return target.EndsWith (s);
244                         }
245                 }
246         }
247 }