2007-11-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / PermissionSetAttribute.cs
1 //
2 // System.Security.Permissions.PermissionSetAttribute.cs
3 //
4 // Authors
5 //      Duncan Mak <duncan@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.Runtime.InteropServices;
33 using System.Security.Policy;
34 using System.Text;
35
36 using Mono.Security.Cryptography;
37 using Mono.Xml;
38
39 namespace System.Security.Permissions {
40
41 #if NET_2_0
42         [ComVisible (true)]
43 #endif
44         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
45                          AttributeTargets.Struct | AttributeTargets.Constructor |
46                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
47         [Serializable]
48         public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
49
50                 // Fields
51                 private string file;
52                 private string name;
53                 private bool isUnicodeEncoded;
54                 private string xml;
55 #if NET_2_0
56                 private string hex;
57 #endif
58                 
59                 // Constructor
60                 public PermissionSetAttribute (SecurityAction action)
61                         : base (action)
62                 {
63                 }
64                 
65                 // Properties
66                 public string File {
67                         get { return file; }
68                         set { file = value; }
69                 }
70 #if NET_2_0
71                 public string Hex {
72                         get { return hex; }
73                         set { hex = value; }
74                 }
75 #endif
76                 public string Name {
77                         get { return name; }
78                         set { name = value; }
79                 }
80
81                 public bool UnicodeEncoded {
82                         get { return isUnicodeEncoded; }
83                         set { isUnicodeEncoded = value; }
84                 }
85
86                 public string XML {
87                         get { return xml; }
88                         set { xml = value; }
89                 }
90                 
91                 // Methods
92                 public override IPermission CreatePermission ()
93                 {
94                         return null;      // Not used, used for inheritance from SecurityAttribute
95                 }
96
97                 private PermissionSet CreateFromXml (string xml) 
98                 {
99                         SecurityParser sp = new SecurityParser ();
100                         try {
101                                 sp.LoadXml (xml);
102                         }
103                         catch (Mono.Xml.SmallXmlParserException xe) {
104                                 throw new XmlSyntaxException (xe.Line, xe.ToString ());
105                         }
106                         SecurityElement se = sp.ToXml ();
107
108                         string className = se.Attribute ("class");
109                         if (className == null)
110                                 return null;
111
112                         PermissionState state = PermissionState.None;
113                         if (CodeAccessPermission.IsUnrestricted (se))
114                                 state = PermissionState.Unrestricted;
115
116                         if (className.EndsWith ("NamedPermissionSet")) {
117                                 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
118                                 nps.FromXml (se);
119                                 return (PermissionSet) nps;
120                         }
121                         else if (className.EndsWith ("PermissionSet")) {
122                                 PermissionSet ps = new PermissionSet (state);
123                                 ps.FromXml (se);
124                                 return ps;
125                         }
126                         return null;
127                 }
128
129                 public PermissionSet CreatePermissionSet ()
130                 {
131                         PermissionSet pset = null;
132                         if (this.Unrestricted)
133                                 pset = new PermissionSet (PermissionState.Unrestricted);
134                         else {
135                                 pset = new PermissionSet (PermissionState.None);
136                                 if (name != null) {
137                                         return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
138                                 }
139                                 else if (file != null) {
140                                         Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
141                                         using (StreamReader sr = new StreamReader (file, e)) {
142                                                 pset = CreateFromXml (sr.ReadToEnd ());
143                                         }
144                                 }
145                                 else if (xml != null) {
146                                         pset = CreateFromXml (xml);
147                                 }
148 #if NET_2_0
149                                 else if (hex != null) {
150                                         // Unicode isn't supported
151                                         //Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
152                                         Encoding e = System.Text.Encoding.ASCII;
153                                         byte[] bin = CryptoConvert.FromHex (hex);
154                                         pset = CreateFromXml (e.GetString (bin, 0, bin.Length));
155                                 }
156 #endif
157                         }
158                         return pset;
159                 }
160         }
161 }