2005-12-05 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlSecureResolver.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 #if NET_1_0
23 #endif
24 #if NET_1_1
25 //
26 // System.Xml.XmlSecureResolver.cs
27 //
28 // Author: Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
29 //
30 // (C) 2003 Atsushi Enomoto
31 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
32 //
33
34 using System.Net;
35 using System.Security;
36 using System.Security.Policy;
37 using System.Security.Permissions;
38
39 namespace System.Xml
40 {
41         public class XmlSecureResolver : XmlResolver
42         {
43
44 #region Static Members
45
46                 public static Evidence CreateEvidenceForUrl (string securityUrl)
47                 {
48                         Evidence e = new Evidence ();
49
50                         if ((securityUrl != null) && (securityUrl.Length > 0)) {
51                                 try {
52                                         Url url = new Url (securityUrl);
53                                         e.AddHost (url);
54                                 } catch (ArgumentException) {
55                                 }
56
57                                 try {
58                                         Zone zone = Zone.CreateFromUrl (securityUrl);
59                                         e.AddHost (zone);
60                                 } catch (ArgumentException) {
61                                 }
62
63                                 try {
64                                         Site site = Site.CreateFromUrl (securityUrl);
65                                         e.AddHost (site);
66                                 } catch (ArgumentException) {
67                                 }
68                         }
69
70                         return e;
71                 }
72 #endregion
73
74                 XmlResolver resolver;
75                 PermissionSet permissionSet;
76
77 #region .ctor and Finalizer
78
79                 public XmlSecureResolver (
80                         XmlResolver resolver, Evidence evidence)
81                 {
82                         this.resolver = resolver;
83                         if (SecurityManager.SecurityEnabled) {
84                                 this.permissionSet = SecurityManager.ResolvePolicy (evidence);
85                         }
86                 }
87
88                 public XmlSecureResolver (
89                         XmlResolver resolver, PermissionSet permissionSet)
90                 {
91                         this.resolver = resolver;
92                         this.permissionSet = permissionSet;
93                 }
94
95                 public XmlSecureResolver (
96                         XmlResolver resolver, string securityUrl)
97                 {
98                         this.resolver = resolver;
99                         if (SecurityManager.SecurityEnabled) {
100                                 this.permissionSet = SecurityManager.ResolvePolicy (CreateEvidenceForUrl (securityUrl));
101                         }
102                 }
103 #endregion
104
105 #region Property
106
107                 public override ICredentials Credentials {
108                         set { resolver.Credentials = value; }
109                 }
110
111 #endregion
112
113 #region Methods
114
115                 [MonoTODO ("CAS: 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 }
138 #endif