2 // System.Security.Policy.Zone
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Runtime.InteropServices;
38 namespace System.Security.Policy {
42 public sealed class Zone :
46 IIdentityPermissionFactory, IBuiltInEvidence {
48 private SecurityZone zone;
50 public Zone (SecurityZone zone)
52 if (!Enum.IsDefined (typeof (SecurityZone), zone)) {
53 string msg = String.Format (Locale.GetText ("Invalid zone {0}."), zone);
54 throw new ArgumentException (msg, "zone");
62 public SecurityZone SecurityZone {
70 return new Zone (zone);
73 public IPermission CreateIdentityPermission (Evidence evidence)
75 return new ZoneIdentityPermission (zone);
78 [MonoTODO ("Not user configurable yet")]
79 public static Zone CreateFromUrl (string url)
82 throw new ArgumentNullException ("url");
84 SecurityZone z = SecurityZone.NoZone;
88 Uri uri = new Uri (url);
89 // TODO: apply zone configuration
90 // this is the only way to use the Trusted and Untrusted zones
92 if (z == SecurityZone.NoZone) {
93 // not part of configuration, the use default mapping
95 if (File.Exists (uri.LocalPath))
96 z = SecurityZone.MyComputer;
97 else if (String.Compare ("FILE://", 0, url, 0, 7, true, CultureInfo.InvariantCulture) == 0)
98 z = SecurityZone.Intranet; // non accessible file://
100 z = SecurityZone.Internet;
102 else if (uri.IsLoopback) { // e.g. http://localhost/x
103 z = SecurityZone.Intranet;
106 // all protocols, including unknown ones
107 z = SecurityZone.Internet;
114 public override bool Equals (object o)
116 Zone z = (o as Zone);
120 return (z.zone == zone);
123 public override int GetHashCode ()
128 public override string ToString ()
130 SecurityElement se = new SecurityElement ("System.Security.Policy.Zone");
131 se.AddAttribute ("version", "1");
132 se.AddChild (new SecurityElement ("Zone", zone.ToString ()));
133 return se.ToString ();
136 // interface IBuiltInEvidence
138 int IBuiltInEvidence.GetRequiredSize (bool verbose)
143 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
145 int new_zone = (int) buffer [position++];
146 new_zone += buffer [position++];
150 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
152 buffer [position++] = '\x0003';
153 buffer [position++] = (char) (((int) zone) >> 16);
154 buffer [position++] = (char) (((int) zone) & 0x0FFFF);