**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Policy / Zone.cs
blobddef56bf328967313c042dd969af772fc5bebfcc
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 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:
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.
31 using System.IO;
32 using System.Globalization;
33 using System.Security.Permissions;
35 using Mono.Security;
37 namespace System.Security.Policy {
39 [Serializable]
40 public sealed class Zone : IIdentityPermissionFactory, IBuiltInEvidence {
41 SecurityZone zone;
43 public Zone (SecurityZone zone)
45 if (!Enum.IsDefined (typeof (SecurityZone), zone)) {
46 string msg = String.Format (Locale.GetText ("Invalid zone {0}."), zone);
47 throw new ArgumentException (msg, "zone");
50 this.zone = zone;
53 // properties
55 public SecurityZone SecurityZone {
56 get { return zone; }
59 // methods
61 public object Copy ()
63 return new Zone (zone);
66 public IPermission CreateIdentityPermission (Evidence evidence)
68 return new ZoneIdentityPermission (zone);
71 [MonoTODO ("Not user configurable yet")]
72 public static Zone CreateFromUrl (string url)
74 if (url == null)
75 throw new ArgumentNullException ("url");
77 SecurityZone z = SecurityZone.NoZone;
78 if (url.Length == 0)
79 return new Zone (z);
81 Uri uri = new Uri (url);
82 // TODO: apply zone configuration
83 // this is the only way to use the Trusted and Untrusted zones
85 if (z == SecurityZone.NoZone) {
86 // not part of configuration, the use default mapping
87 if (uri.IsFile) {
88 if (File.Exists (uri.LocalPath))
89 z = SecurityZone.MyComputer;
90 else
91 z = SecurityZone.Intranet; // non accessible file://
93 else if (uri.IsLoopback) { // e.g. http://localhost/x
94 z = SecurityZone.Intranet;
96 else {
97 // all protocols, including unknown ones
98 z = SecurityZone.Internet;
102 return new Zone (z);
105 public override bool Equals (object o)
107 if (!(o is Zone))
108 return false;
110 return (((Zone) o).zone == zone);
113 public override int GetHashCode ()
115 return (int) zone;
118 public override string ToString ()
120 SecurityElement se = new SecurityElement ("System.Security.Policy.Zone");
121 se.AddAttribute ("version", "1");
122 se.AddChild (new SecurityElement ("Zone", zone.ToString ()));
123 return se.ToString ();
126 // interface IBuiltInEvidence
128 int IBuiltInEvidence.GetRequiredSize (bool verbose)
130 return 3;
133 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) {
134 int new_zone = (int) buffer [position++];
135 new_zone += buffer [position++];
136 return position;
139 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
141 buffer [position++] = '\x0003';
142 buffer [position++] = (char) (((int) zone) >> 16);
143 buffer [position++] = (char) (((int) zone) & 0x0FFFF);
144 return position;