BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.XML / System.Xml / XmlSecureResolver.cs
1 //
2 // System.Xml.XmlSecureResolver.cs
3 //
4 // Author: Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
5 //
6 // (C) 2003 Atsushi Enomoto
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
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.Net;
32 using System.Security;
33 using System.Security.Policy;
34 using System.Security.Permissions;
35
36 namespace System.Xml
37 {
38         public class XmlSecureResolver : XmlResolver
39         {
40
41 #region Static Members
42
43                 public static Evidence CreateEvidenceForUrl (string securityUrl)
44                 {
45                         Evidence e = new Evidence ();
46
47                         if ((securityUrl != null) && (securityUrl.Length > 0)) {
48                                 try {
49                                         Url url = new Url (securityUrl);
50                                         e.AddHost (url);
51                                 } catch (ArgumentException) {
52                                 }
53
54                                 try {
55                                         Zone zone = Zone.CreateFromUrl (securityUrl);
56                                         e.AddHost (zone);
57                                 } catch (ArgumentException) {
58                                 }
59
60                                 try {
61                                         Site site = Site.CreateFromUrl (securityUrl);
62                                         e.AddHost (site);
63                                 } catch (ArgumentException) {
64                                 }
65                         }
66
67                         return e;
68                 }
69 #endregion
70
71                 XmlResolver resolver;
72                 PermissionSet permissionSet;
73
74 #region .ctor and Finalizer
75
76                 public XmlSecureResolver (
77                         XmlResolver resolver, Evidence evidence)
78                 {
79                         this.resolver = resolver;
80                         if (SecurityManager.SecurityEnabled) {
81                                 this.permissionSet = SecurityManager.ResolvePolicy (evidence);
82                         }
83                 }
84
85                 public XmlSecureResolver (
86                         XmlResolver resolver, PermissionSet permissionSet)
87                 {
88                         this.resolver = resolver;
89                         this.permissionSet = permissionSet;
90                 }
91
92                 public XmlSecureResolver (
93                         XmlResolver resolver, string securityUrl)
94                 {
95                         this.resolver = resolver;
96                         if (SecurityManager.SecurityEnabled) {
97                                 this.permissionSet = SecurityManager.ResolvePolicy (CreateEvidenceForUrl (securityUrl));
98                         }
99                 }
100 #endregion
101
102 #region Property
103
104 #if !NET_2_1
105                 public override ICredentials Credentials {
106                         set { resolver.Credentials = value; }
107                 }
108 #endif
109
110 #endregion
111
112 #region Methods
113
114                 [MonoTODO]
115                 // FIXME: imperative PermitOnly isn't supported
116                 public override object GetEntity (
117                         Uri absoluteUri, string role, Type ofObjectToReturn)
118                 {
119                         if (SecurityManager.SecurityEnabled) {
120                                 // in case the security manager was switched after the constructor was called
121                                 if (permissionSet == null) {
122                                         throw new SecurityException (Locale.GetText (
123                                                 "Security Manager wasn't active when instance was created."));
124                                 }
125                                 permissionSet.PermitOnly ();
126                         }
127                         return resolver.GetEntity (absoluteUri, role, ofObjectToReturn);
128                 }
129
130                 public override Uri ResolveUri (Uri baseUri, string relativeUri)
131                 {
132                         return resolver.ResolveUri (baseUri, relativeUri);
133                 }
134 #endregion
135
136         }
137 }