Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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 #if !MOONLIGHT
32
33 using System.IO;
34 using System.Globalization;
35 using System.Security.Permissions;
36 using System.Runtime.InteropServices;
37
38 using Mono.Security;
39
40 namespace System.Security.Policy {
41
42         [Serializable]
43         [ComVisible (true)]
44         public sealed class Zone :
45 #if NET_4_0
46                 EvidenceBase,
47 #endif
48                 IIdentityPermissionFactory, IBuiltInEvidence    {
49
50                 private SecurityZone zone;
51                 
52                 public Zone (SecurityZone zone)
53                 {
54                         if (!Enum.IsDefined (typeof (SecurityZone), zone)) {
55                                 string msg = String.Format (Locale.GetText ("Invalid zone {0}."), zone);
56                                 throw new ArgumentException (msg, "zone");
57                         }
58
59                         this.zone = zone;
60                 }
61
62                 // properties
63
64                 public SecurityZone SecurityZone {
65                         get { return zone; }
66                 }
67
68                 // methods
69
70                 public object Copy ()
71                 {
72                         return new Zone (zone);
73                 }
74
75                 public IPermission CreateIdentityPermission (Evidence evidence)
76                 {
77                         return new ZoneIdentityPermission (zone);
78                 }
79
80                 [MonoTODO ("Not user configurable yet")]
81                 public static Zone CreateFromUrl (string url)
82                 {
83                         if (url == null)
84                                 throw new ArgumentNullException ("url");
85
86                         SecurityZone z = SecurityZone.NoZone;
87                         if (url.Length == 0)
88                                 return new Zone (z);
89
90                         Uri uri = new Uri (url);
91                         // TODO: apply zone configuration
92                         // this is the only way to use the Trusted and Untrusted zones
93
94                         if (z == SecurityZone.NoZone) {
95                                 // not part of configuration, the use default mapping
96                                 if (uri.IsFile) {
97                                         if (File.Exists (uri.LocalPath))
98                                                 z = SecurityZone.MyComputer;
99                                         else if (String.Compare ("FILE://", 0, url, 0, 7, true, CultureInfo.InvariantCulture) == 0)
100                                                 z = SecurityZone.Intranet;      // non accessible file:// 
101                                         else
102                                                 z = SecurityZone.Internet;
103                                 }
104                                 else if (uri.IsLoopback) {                      // e.g. http://localhost/x
105                                         z = SecurityZone.Intranet;
106                                 }
107                                 else {
108                                         // all protocols, including unknown ones
109                                         z = SecurityZone.Internet;
110                                 }
111                         }
112
113                         return new Zone (z);
114                 }
115
116                 public override bool Equals (object o)
117                 {
118                         Zone z = (o as Zone);
119                         if (z == null)
120                                 return false;
121
122                         return (z.zone == zone);
123                 }
124
125                 public override int GetHashCode ()
126                 {
127                         return (int) zone;
128                 }
129
130                 public override string ToString ()
131                 {
132                         SecurityElement se = new SecurityElement ("System.Security.Policy.Zone");
133                         se.AddAttribute ("version", "1");
134                         se.AddChild (new SecurityElement ("Zone", zone.ToString ()));
135                         return se.ToString ();
136                 }
137
138                 // interface IBuiltInEvidence
139
140                 int IBuiltInEvidence.GetRequiredSize (bool verbose)
141                 {
142                         return 3;
143                 }
144
145                 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
146                 {
147                         int new_zone = (int) buffer [position++];
148                         new_zone += buffer [position++];
149                         return position;
150                 }
151
152                 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
153                 {
154                         buffer [position++] = '\x0003';
155                         buffer [position++] = (char) (((int) zone) >> 16);
156                         buffer [position++] = (char) (((int) zone) & 0x0FFFF);
157                         return position;
158                 }
159         }
160 }
161
162 #endif
163