Merge pull request #3446 from lambdageek/bug-42584
[mono.git] / mcs / class / corlib / System.Security.Policy / Zone.cs
1 //
2 // System.Security.Policy.Zone
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
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 Zone :
43                 EvidenceBase,
44                 IIdentityPermissionFactory, IBuiltInEvidence    {
45
46                 private SecurityZone zone;
47                 
48                 public Zone (SecurityZone zone)
49                 {
50                         if (!Enum.IsDefined (typeof (SecurityZone), zone)) {
51                                 string msg = String.Format (Locale.GetText ("Invalid zone {0}."), zone);
52                                 throw new ArgumentException (msg, "zone");
53                         }
54
55                         this.zone = zone;
56                 }
57
58                 // properties
59
60                 public SecurityZone SecurityZone {
61                         get { return zone; }
62                 }
63
64                 // methods
65
66                 public object Copy ()
67                 {
68                         return new Zone (zone);
69                 }
70
71                 public IPermission CreateIdentityPermission (Evidence evidence)
72                 {
73                         return new ZoneIdentityPermission (zone);
74                 }
75
76                 [MonoTODO ("Not user configurable yet")]
77                 public static Zone CreateFromUrl (string url)
78                 {
79                         if (url == null)
80                                 throw new ArgumentNullException ("url");
81
82                         SecurityZone z = SecurityZone.NoZone;
83                         if (url.Length == 0)
84                                 return new Zone (z);
85
86                         Uri uri = null;
87                         try {
88                                 uri = new Uri (url);
89                         }
90                         catch {
91                                 return new Zone (z);
92                         }
93                         // TODO: apply zone configuration
94                         // this is the only way to use the Trusted and Untrusted zones
95
96                         if (z == SecurityZone.NoZone) {
97                                 // not part of configuration, the use default mapping
98                                 if (uri.IsFile) {
99                                         if (File.Exists (uri.LocalPath))
100                                                 z = SecurityZone.MyComputer;
101                                         else if (String.Compare ("FILE://", 0, url, 0, 7, true, CultureInfo.InvariantCulture) == 0)
102                                                 z = SecurityZone.Intranet;      // non accessible file:// 
103                                         else
104                                                 z = SecurityZone.Internet;
105                                 }
106                                 else if (uri.IsLoopback) {                      // e.g. http://localhost/x
107                                         z = SecurityZone.Intranet;
108                                 }
109                                 else {
110                                         // all protocols, including unknown ones
111                                         z = SecurityZone.Internet;
112                                 }
113                         }
114
115                         return new Zone (z);
116                 }
117
118                 public override bool Equals (object o)
119                 {
120                         Zone z = (o as Zone);
121                         if (z == null)
122                                 return false;
123
124                         return (z.zone == zone);
125                 }
126
127                 public override int GetHashCode ()
128                 {
129                         return (int) zone;
130                 }
131
132                 public override string ToString ()
133                 {
134                         SecurityElement se = new SecurityElement ("System.Security.Policy.Zone");
135                         se.AddAttribute ("version", "1");
136                         se.AddChild (new SecurityElement ("Zone", zone.ToString ()));
137                         return se.ToString ();
138                 }
139
140                 // interface IBuiltInEvidence
141
142                 int IBuiltInEvidence.GetRequiredSize (bool verbose)
143                 {
144                         return 3;
145                 }
146
147                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
148                 {
149                         int new_zone = (int) buffer [position++];
150                         new_zone += buffer [position++];
151                         return position;
152                 }
153
154                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
155                 {
156                         buffer [position++] = '\x0003';
157                         buffer [position++] = (char) (((int) zone) >> 16);
158                         buffer [position++] = (char) (((int) zone) & 0x0FFFF);
159                         return position;
160                 }
161         }
162 }