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