ResourcePermissionBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // System.Security.Permissions.ResourcePermissionBase.cs
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2002
  9. // Copyright (C) 2004 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. using System.Collections;
  31. using System.Globalization;
  32. namespace System.Security.Permissions {
  33. [Serializable]
  34. public abstract class ResourcePermissionBase : CodeAccessPermission, IUnrestrictedPermission {
  35. private const int version = 1;
  36. private ArrayList _list;
  37. private bool _unrestricted;
  38. private Type _type;
  39. private string[] _tags;
  40. protected ResourcePermissionBase ()
  41. {
  42. _list = new ArrayList ();
  43. }
  44. protected ResourcePermissionBase (PermissionState state) : this ()
  45. {
  46. // there are no validation of the permission state
  47. _unrestricted = (state == PermissionState.Unrestricted);
  48. // but any invalid value results in a restricted set
  49. }
  50. public const string Any = "*";
  51. public const string Local = ".";
  52. protected Type PermissionAccessType {
  53. get { return _type; }
  54. set {
  55. if (value == null)
  56. throw new ArgumentNullException ("PermissionAccessType");
  57. if (!value.IsEnum)
  58. throw new ArgumentException ("!Enum", "PermissionAccessType");
  59. _type = value;
  60. }
  61. }
  62. protected string[] TagNames {
  63. get { return _tags; }
  64. set {
  65. if (value == null)
  66. throw new ArgumentNullException ("TagNames");
  67. if (value.Length == 0)
  68. throw new ArgumentException ("Length==0", "TagNames");
  69. _tags = value;
  70. }
  71. }
  72. protected void AddPermissionAccess (ResourcePermissionBaseEntry entry)
  73. {
  74. CheckEntry (entry);
  75. if (Exists (entry)) {
  76. string msg = Locale.GetText ("Entry already exists.");
  77. throw new InvalidOperationException (msg);
  78. }
  79. _list.Add (entry);
  80. }
  81. protected void Clear ()
  82. {
  83. _list.Clear ();
  84. }
  85. public override IPermission Copy ()
  86. {
  87. ResourcePermissionBase copy = CreateFromType (this.GetType (), _unrestricted);
  88. if (_tags != null)
  89. copy._tags = (string[]) _tags.Clone ();
  90. copy._type = _type;
  91. // FIXME: shallow or deep copy ?
  92. copy._list.AddRange (_list);
  93. return copy;
  94. }
  95. [MonoTODO ("incomplete - need more test")]
  96. public override void FromXml (SecurityElement securityElement)
  97. {
  98. // duplicate MS behaviour - reported as FDBK15052
  99. if (securityElement == null)
  100. throw new NullReferenceException ("securityElement");
  101. CheckSecurityElement (securityElement, "securityElement", version, version);
  102. // Note: we do not (yet) care about the return value
  103. // as we only accept version 1 (min/max values)
  104. _unrestricted = PermissionHelper.IsUnrestricted (securityElement);
  105. // TODO
  106. }
  107. protected ResourcePermissionBaseEntry[] GetPermissionEntries ()
  108. {
  109. ResourcePermissionBaseEntry[] entries = new ResourcePermissionBaseEntry [_list.Count];
  110. _list.CopyTo (entries, 0);
  111. return entries;
  112. }
  113. public override IPermission Intersect (IPermission target)
  114. {
  115. ResourcePermissionBase rpb = Cast (target);
  116. if (rpb == null)
  117. return null;
  118. bool su = this.IsUnrestricted ();
  119. bool tu = rpb.IsUnrestricted ();
  120. // if one is empty we return null (unless the other one is unrestricted)
  121. if (IsEmpty () && !tu)
  122. return null;
  123. if (rpb.IsEmpty () && !su)
  124. return null;
  125. ResourcePermissionBase result = CreateFromType (this.GetType (), (su && tu));
  126. foreach (ResourcePermissionBaseEntry entry in _list) {
  127. if (tu || rpb.Exists (entry))
  128. result.AddPermissionAccess (entry);
  129. }
  130. foreach (ResourcePermissionBaseEntry entry in rpb._list) {
  131. // don't add twice
  132. if ((su || this.Exists (entry)) && !result.Exists (entry))
  133. result.AddPermissionAccess (entry);
  134. }
  135. return result;
  136. }
  137. public override bool IsSubsetOf (IPermission target)
  138. {
  139. // do not use Cast - different permissions return false :-/
  140. if (target == null)
  141. return true;
  142. ResourcePermissionBase rpb = (target as ResourcePermissionBase);
  143. if (rpb == null)
  144. return false;
  145. if (rpb.IsUnrestricted ())
  146. return true;
  147. if (IsUnrestricted ())
  148. return rpb.IsUnrestricted ();
  149. foreach (ResourcePermissionBaseEntry entry in _list) {
  150. if (!rpb.Exists (entry))
  151. return false;
  152. }
  153. return true;
  154. }
  155. public bool IsUnrestricted ()
  156. {
  157. return _unrestricted;
  158. }
  159. protected void RemovePermissionAccess (ResourcePermissionBaseEntry entry)
  160. {
  161. CheckEntry (entry);
  162. for (int i = 0; i < _list.Count; i++) {
  163. ResourcePermissionBaseEntry rpbe = (ResourcePermissionBaseEntry) _list [i];
  164. if (Equals (entry, rpbe)) {
  165. _list.RemoveAt (i);
  166. return;
  167. }
  168. }
  169. string msg = Locale.GetText ("Entry doesn't exists.");
  170. throw new InvalidOperationException (msg);
  171. }
  172. public override SecurityElement ToXml ()
  173. {
  174. SecurityElement se = PermissionHelper.Element (this.GetType (), version);
  175. if (IsUnrestricted ()) {
  176. se.AddAttribute ("Unrestricted", "true");
  177. }
  178. else {
  179. foreach (ResourcePermissionBaseEntry entry in _list) {
  180. SecurityElement container = se;
  181. for (int i=0; i < _tags.Length; i++) {
  182. SecurityElement child = new SecurityElement (_tags [i]);
  183. child.AddAttribute ("name", entry.PermissionAccessPath [i]);
  184. container.AddChild (child);
  185. child = container;
  186. }
  187. }
  188. }
  189. return se;
  190. }
  191. public override IPermission Union (IPermission target)
  192. {
  193. ResourcePermissionBase rpb = Cast (target);
  194. if (rpb == null)
  195. return Copy ();
  196. if (IsEmpty () && rpb.IsEmpty ())
  197. return null;
  198. if (rpb.IsEmpty ())
  199. return Copy ();
  200. if (IsEmpty ())
  201. return rpb.Copy ();
  202. bool unrestricted = (IsUnrestricted () || rpb.IsUnrestricted ());
  203. ResourcePermissionBase result = CreateFromType (this.GetType (), unrestricted);
  204. // strangely unrestricted union doesn't process the elements (while intersect does)
  205. if (!unrestricted) {
  206. foreach (ResourcePermissionBaseEntry entry in _list) {
  207. result.AddPermissionAccess (entry);
  208. }
  209. foreach (ResourcePermissionBaseEntry entry in rpb._list) {
  210. // don't add twice
  211. if (!result.Exists (entry))
  212. result.AddPermissionAccess (entry);
  213. }
  214. }
  215. return result;
  216. }
  217. // helpers
  218. private bool IsEmpty ()
  219. {
  220. return (!_unrestricted && (_list.Count == 0));
  221. }
  222. private ResourcePermissionBase Cast (IPermission target)
  223. {
  224. if (target == null)
  225. return null;
  226. ResourcePermissionBase rp = (target as ResourcePermissionBase);
  227. if (rp == null) {
  228. PermissionHelper.ThrowInvalidPermission (target, typeof (ResourcePermissionBase));
  229. }
  230. return rp;
  231. }
  232. internal void CheckEntry (ResourcePermissionBaseEntry entry)
  233. {
  234. if (entry == null)
  235. throw new ArgumentNullException ("entry");
  236. if ((entry.PermissionAccessPath == null) || (entry.PermissionAccessPath.Length != _tags.Length)) {
  237. string msg = Locale.GetText ("Entry doesn't match TagNames");
  238. throw new InvalidOperationException (msg);
  239. }
  240. }
  241. internal bool Equals (ResourcePermissionBaseEntry entry1, ResourcePermissionBaseEntry entry2)
  242. {
  243. if (entry1.PermissionAccess != entry2.PermissionAccess)
  244. return false;
  245. if (entry1.PermissionAccessPath.Length != entry2.PermissionAccessPath.Length)
  246. return false;
  247. for (int i=0; i < entry1.PermissionAccessPath.Length; i++) {
  248. if (entry1.PermissionAccessPath [i] != entry2.PermissionAccessPath [i])
  249. return false;
  250. }
  251. return true;
  252. }
  253. internal bool Exists (ResourcePermissionBaseEntry entry)
  254. {
  255. if (_list.Count == 0)
  256. return false;
  257. foreach (ResourcePermissionBaseEntry rpbe in _list) {
  258. if (Equals (rpbe, entry))
  259. return true;
  260. }
  261. return false;
  262. }
  263. // logic isn't identical to PermissionHelper.CheckSecurityElement
  264. // - no throw on version mismatch
  265. internal int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
  266. {
  267. if (se == null)
  268. throw new ArgumentNullException (parameterName);
  269. // Note: we do not care about the class attribute at
  270. // this stage (in fact we don't even if the class
  271. // attribute is present or not). Anyway the object has
  272. // already be created, with success, if we're loading it
  273. // we assume minimum version if no version number is supplied
  274. int version = minimumVersion;
  275. string v = se.Attribute ("version");
  276. if (v != null) {
  277. try {
  278. version = Int32.Parse (v);
  279. }
  280. catch (Exception e) {
  281. string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
  282. msg = String.Format (msg, v);
  283. throw new ArgumentException (msg, parameterName, e);
  284. }
  285. }
  286. return version;
  287. }
  288. // static helpers
  289. private static char[] invalidChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\\', '\x160' };
  290. internal static void ValidateMachineName (string name)
  291. {
  292. // FIXME: maybe other checks are required (but not documented)
  293. if ((name == null) || (name.Length == 0) || (name.IndexOfAny (invalidChars) != -1)) {
  294. string msg = Locale.GetText ("Invalid machine name '{0}'.");
  295. if (name == null)
  296. name = "(null)";
  297. msg = String.Format (msg, name);
  298. throw new ArgumentException (msg, "MachineName");
  299. }
  300. }
  301. internal static ResourcePermissionBase CreateFromType (Type type, bool unrestricted)
  302. {
  303. object[] parameters = new object [1];
  304. parameters [0] = (object) ((unrestricted) ? PermissionState.Unrestricted : PermissionState.None);
  305. // we must return the derived type - this is why an empty constructor is required ;-)
  306. return (ResourcePermissionBase) Activator.CreateInstance (type, parameters);
  307. }
  308. }
  309. }