CrossDomainPolicyManager.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // CrossDomainPolicyManager.cs
  3. //
  4. // Authors:
  5. // Atsushi Enomoto <[email protected]>
  6. // Moonlight List ([email protected])
  7. //
  8. // Copyright (C) 2009-2010 Novell, Inc. http://www.novell.com
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_1
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Net.Sockets;
  34. using System.Reflection;
  35. using System.Security;
  36. using System.Text;
  37. using System.Threading;
  38. namespace System.Net.Policy {
  39. internal static class CrossDomainPolicyManager {
  40. public static string GetRoot (Uri uri)
  41. {
  42. if ((uri.Scheme == "http" && uri.Port == 80) || (uri.Scheme == "https" && uri.Port == 443) || (uri.Port == -1))
  43. return String.Format ("{0}://{1}/", uri.Scheme, uri.DnsSafeHost);
  44. else
  45. return String.Format ("{0}://{1}:{2}/", uri.Scheme, uri.DnsSafeHost, uri.Port);
  46. }
  47. #if !TEST
  48. public const string ClientAccessPolicyFile = "/clientaccesspolicy.xml";
  49. public const string CrossDomainFile = "/crossdomain.xml";
  50. const int Timeout = 10000;
  51. // Web Access Policy
  52. static Dictionary<string,ICrossDomainPolicy> policies = new Dictionary<string,ICrossDomainPolicy> ();
  53. static internal ICrossDomainPolicy PolicyDownloadPolicy = new PolicyDownloadPolicy ();
  54. static ICrossDomainPolicy site_of_origin_policy = new SiteOfOriginPolicy ();
  55. static ICrossDomainPolicy no_access_policy = new NoAccessPolicy ();
  56. static Uri GetRootUri (Uri uri)
  57. {
  58. return new Uri (GetRoot (uri));
  59. }
  60. public static Uri GetSilverlightPolicyUri (Uri uri)
  61. {
  62. return new Uri (GetRootUri (uri), CrossDomainPolicyManager.ClientAccessPolicyFile);
  63. }
  64. public static Uri GetFlashPolicyUri (Uri uri)
  65. {
  66. return new Uri (GetRootUri (uri), CrossDomainPolicyManager.CrossDomainFile);
  67. }
  68. public static ICrossDomainPolicy GetCachedWebPolicy (Uri uri)
  69. {
  70. // if we request an Uri from the same site then we return an "always positive" policy
  71. if (SiteOfOriginPolicy.HasSameOrigin (uri, BaseDomainPolicy.ApplicationUri))
  72. return site_of_origin_policy;
  73. // otherwise we search for an already downloaded policy for the web site
  74. string root = GetRoot (uri);
  75. ICrossDomainPolicy policy = null;
  76. policies.TryGetValue (root, out policy);
  77. // and we return it (if we have it) or null (if we dont)
  78. return policy;
  79. }
  80. private static void AddPolicy (Uri responseUri, ICrossDomainPolicy policy)
  81. {
  82. string root = GetRoot (responseUri);
  83. try {
  84. policies.Add (root, policy);
  85. }
  86. catch (ArgumentException) {
  87. // it's possible another request already added this root
  88. }
  89. }
  90. public static ICrossDomainPolicy BuildSilverlightPolicy (HttpWebResponse response)
  91. {
  92. // return null if no Silverlight policy was found, since we offer a second chance with a flash policy
  93. if ((response.StatusCode != HttpStatusCode.OK) || !response.ContentType.EndsWith ("/xml"))
  94. return null;
  95. ICrossDomainPolicy policy = null;
  96. try {
  97. policy = ClientAccessPolicy.FromStream (response.GetResponseStream ());
  98. if (policy != null)
  99. AddPolicy (response.ResponseUri, policy);
  100. } catch (Exception ex) {
  101. Console.WriteLine (String.Format ("CrossDomainAccessManager caught an exception while reading {0}: {1}",
  102. response.ResponseUri, ex));
  103. // and ignore.
  104. }
  105. return policy;
  106. }
  107. public static ICrossDomainPolicy BuildFlashPolicy (HttpWebResponse response)
  108. {
  109. ICrossDomainPolicy policy = null;
  110. if ((response.StatusCode == HttpStatusCode.OK) && response.ContentType.EndsWith ("/xml")) {
  111. try {
  112. policy = FlashCrossDomainPolicy.FromStream (response.GetResponseStream ());
  113. } catch (Exception ex) {
  114. Console.WriteLine (String.Format ("CrossDomainAccessManager caught an exception while reading {0}: {1}",
  115. response.ResponseUri, ex));
  116. // and ignore.
  117. }
  118. if (policy != null) {
  119. // see DRT# 864 and 865
  120. string site_control = response.Headers ["X-Permitted-Cross-Domain-Policies"];
  121. if (!String.IsNullOrEmpty (site_control))
  122. (policy as FlashCrossDomainPolicy).SiteControl = site_control;
  123. }
  124. }
  125. // the flash policy was the last chance, keep a NoAccess into the cache
  126. if (policy == null)
  127. policy = no_access_policy;
  128. AddPolicy (response.ResponseUri, policy);
  129. return policy;
  130. }
  131. // Socket Policy
  132. //
  133. // - we connect once to a site for the entire application life time
  134. // - this returns us a policy file (silverlight format only) or else no access is granted
  135. // - this policy file
  136. // - can contain multiple policies
  137. // - can apply to multiple domains
  138. // - can grant access to several resources
  139. static Dictionary<string,ClientAccessPolicy> socket_policies = new Dictionary<string,ClientAccessPolicy> ();
  140. static byte [] socket_policy_file_request = Encoding.UTF8.GetBytes ("<policy-file-request/>");
  141. const int PolicyPort = 943;
  142. // make sure this work in a IPv6-only environment
  143. static AddressFamily GetBestFamily ()
  144. {
  145. if (Socket.OSSupportsIPv4)
  146. return AddressFamily.InterNetwork;
  147. else if (Socket.OSSupportsIPv6)
  148. return AddressFamily.InterNetworkV6;
  149. else
  150. return AddressFamily.Unspecified;
  151. }
  152. static Stream GetPolicyStream (IPEndPoint endpoint)
  153. {
  154. MemoryStream ms = new MemoryStream ();
  155. ManualResetEvent mre = new ManualResetEvent (false);
  156. // Silverlight only support TCP
  157. Socket socket = new Socket (GetBestFamily (), SocketType.Stream, ProtocolType.Tcp);
  158. // Application code can't connect to port 943, so we need a special/internal API/ctor to allow this
  159. SocketAsyncEventArgs saea = new SocketAsyncEventArgs (true);
  160. saea.RemoteEndPoint = new IPEndPoint (endpoint.Address, PolicyPort);
  161. saea.Completed += delegate (object sender, SocketAsyncEventArgs e) {
  162. if (e.SocketError != SocketError.Success) {
  163. mre.Set ();
  164. return;
  165. }
  166. switch (e.LastOperation) {
  167. case SocketAsyncOperation.Connect:
  168. e.SetBuffer (socket_policy_file_request, 0, socket_policy_file_request.Length);
  169. socket.SendAsync (e);
  170. break;
  171. case SocketAsyncOperation.Send:
  172. byte [] buffer = new byte [256];
  173. e.SetBuffer (buffer, 0, buffer.Length);
  174. socket.ReceiveAsync (e);
  175. break;
  176. case SocketAsyncOperation.Receive:
  177. int transfer = e.BytesTransferred;
  178. if (transfer > 0) {
  179. ms.Write (e.Buffer, 0, transfer);
  180. // Console.Write (Encoding.UTF8.GetString (e.Buffer, 0, transfer));
  181. }
  182. if ((transfer == 0) || (transfer < e.Buffer.Length)) {
  183. ms.Position = 0;
  184. mre.Set ();
  185. } else {
  186. socket.ReceiveAsync (e);
  187. }
  188. break;
  189. }
  190. };
  191. socket.ConnectAsync (saea);
  192. // behave like there's no policy (no socket access) if we timeout
  193. if (!mre.WaitOne (Timeout))
  194. return null;
  195. return ms;
  196. }
  197. public static ClientAccessPolicy CreateForEndPoint (IPEndPoint endpoint)
  198. {
  199. Stream s = GetPolicyStream (endpoint);
  200. if (s == null)
  201. return null;
  202. ClientAccessPolicy policy = null;
  203. try {
  204. policy = (ClientAccessPolicy) ClientAccessPolicy.FromStream (s);
  205. } catch (Exception ex) {
  206. Console.WriteLine (String.Format ("CrossDomainAccessManager caught an exception while reading {0}: {1}",
  207. endpoint, ex.Message));
  208. // and ignore.
  209. }
  210. return policy;
  211. }
  212. static public bool CheckEndPoint (EndPoint endpoint)
  213. {
  214. // if needed transform the DnsEndPoint into a usable IPEndPoint
  215. IPEndPoint ip = (endpoint as IPEndPoint);
  216. if (ip == null)
  217. throw new ArgumentException ("endpoint");
  218. // find the policy (cached or to be downloaded) associated with the endpoint
  219. string address = ip.Address.ToString ();
  220. ClientAccessPolicy policy = null;
  221. if (!socket_policies.TryGetValue (address, out policy)) {
  222. policy = CreateForEndPoint (ip);
  223. socket_policies.Add (address, policy);
  224. }
  225. // no access granted if no policy is available
  226. if (policy == null)
  227. return false;
  228. // does the policy allows access ?
  229. return policy.IsAllowed (ip);
  230. }
  231. #endif
  232. }
  233. }
  234. #endif