2004-08-08 Sebastien Pouliot <sebastien@ximian.com>
[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 (spouliot@motus.com)
7 //
8 // (C) 2003 Ximian, Inc (http://www.ximian.com)
9 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Globalization;
37 using System.Security.Permissions;
38 using System.Security.Policy;
39
40 namespace System.Security.Policy {
41
42         [Serializable]
43         public sealed class Site: IIdentityPermissionFactory, IBuiltInEvidence {
44
45                 internal string origin_site;
46
47                 public Site (string name)
48                 {
49                         if (name == null)
50                                 throw new ArgumentNullException (Locale.GetText ("name is null"));
51                         if (!IsValid (name))
52                                 throw new ArgumentException (Locale.GetText ("name is not valid"));
53                         
54                         origin_site = name;
55                 }
56
57                 public static Site CreateFromUrl (string url)
58                 {
59                         return new Site (url);
60                 }
61
62                 public object Copy ()
63                 {
64                         return new Site (origin_site);
65                 }
66
67                 public IPermission CreateIdentityPermission (Evidence evidence)
68                 {
69                         return new SiteIdentityPermission (origin_site);
70                 }
71
72                 public override bool Equals (object o)
73                 {
74                         if (o is System.Security.Policy.Site) {
75                                 return (String.Compare (((Site) o).Name, origin_site, true, CultureInfo.InvariantCulture) == 0);
76                         }
77                         return false;
78                 }
79
80                 public override int GetHashCode ()
81                 {
82                         return origin_site.GetHashCode ();
83                 }
84
85                 public override string ToString ()
86                 {
87                         SecurityElement element = new SecurityElement (typeof (System.Security.Policy.Site).FullName);
88                         element.AddAttribute ("version", "1");
89                         element.AddChild (new SecurityElement ("Name", origin_site));
90                         return element.ToString ();
91                 }
92
93                 // properties
94
95                 public string Name {
96                         get { return origin_site; }
97                 }
98
99                 // interface IBuiltInEvidence
100
101                 [MonoTODO]
102                 int IBuiltInEvidence.GetRequiredSize (bool verbose) 
103                 {
104                         return 0;
105                 }
106
107                 [MonoTODO]
108                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
109                 {
110                         return 0;
111                 }
112
113                 [MonoTODO]
114                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
115                 {
116                         return 0;
117                 }
118
119                 // internals
120
121                 internal static bool IsValid (string name)
122                 {
123                         if (name == String.Empty)
124                                 return false;
125                         if ((name.Length == 1) && (name == "."))                // split would remove .
126                                 return false;
127
128                         string [] parts = name.Split ('.');
129                         for (int i=0; i < parts.Length; i++) {
130                                 string part = parts [i];
131                                 if ((i == 0) && (part == "*"))                  // * (only in first part)
132                                         continue;
133                                 foreach (char c in part) {
134                                         int x = Convert.ToInt32 (c);
135                                         bool result = ((x == 45)                // -
136                                                 || (x >= 47 && x <= 57)         // /,0-9
137                                                 || (x >= 64 && x <= 90)         // @,A-Z
138                                                 || (x == 95)                    // _
139                                                 || (x >= 97 && x <= 122));      // a-z
140                                         if (!result)
141                                                 return false;
142                                 }
143                         }
144                         return true;
145                 }
146         }
147 }