2004-04-02 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.Security / SecurityManager.cs
index c5ec392b28a75045b6aa18c0bdfe3d11f5f975fb..c0238669582ad18006b2429f0bb6a9ce2e187c35 100644 (file)
-//\r
-// System.Security.SecurityManager.cs\r
-//\r
-// Author:\r
-//   Nick Drochak(ndrochak@gol.com)\r
-//\r
-// (C) Nick Drochak\r
-//\r
-\r
-using System.Security.Policy;\r
-using System.Collections;\r
-\r
-namespace System.Security {\r
-\r
-       public sealed class SecurityManager  {\r
-               private static bool checkExecutionRights;\r
-               private static bool securityEnabled;\r
-\r
-               public static bool CheckExecutionRights {\r
-                       get{\r
-                               return checkExecutionRights;\r
-                       }\r
-                       set{\r
-                               checkExecutionRights = value;\r
-                       }\r
-               }\r
-\r
-               public static bool SecurityEnabled {\r
-                       get{\r
-                               return securityEnabled;\r
-                       }\r
-                       set{\r
-                               securityEnabled = value;\r
-                       }\r
-               }\r
-\r
-               public static bool IsGranted(IPermission perm){\r
-                       return false;\r
-               }\r
-\r
-               public static PolicyLevel LoadPolicyLevelFromFile(\r
-                       string path, \r
-                       PolicyLevelType type)\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public static PolicyLevel LoadPolicyLevelFromString(\r
-                       string str, \r
-                       PolicyLevelType type)\r
-               {\r
-                       if (null == str){    \r
-                               throw new ArgumentNullException("str");\r
-                       }\r
-                       return null;\r
-               }\r
-\r
-               public static IEnumerator PolicyHierarchy(){\r
-                       return null;\r
-               }\r
-\r
-               public static PermissionSet ResolvePolicy(Evidence evidence){\r
-                       return null;\r
-               }\r
-\r
-               public static PermissionSet ResolvePolicy(\r
-                       Evidence evidence,\r
-                       PermissionSet reqdPset,\r
-                       PermissionSet optPset,\r
-                       PermissionSet denyPset,\r
-                       out PermissionSet denied)\r
-               {\r
-                       denied = null;\r
-                       return null;\r
-               }\r
-\r
-               public static IEnumerator ResolvePolicyGroups(Evidence evidence){\r
-                       return null;\r
-               }\r
-\r
-               public static void SavePolicy(){}\r
-\r
-               public static void SavePolicyLevel(PolicyLevel level){}\r
-\r
-       }\r
-}
\ No newline at end of file
+//
+// System.Security.SecurityManager.cs
+//
+// Authors:
+//     Nick Drochak(ndrochak@gol.com)
+//     Sebastien Pouliot (spouliot@motus.com)
+//
+// (C) Nick Drochak
+// Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
+//
+
+using System.Collections;
+using System.Globalization;
+using System.IO;
+using System.Security.Permissions;
+using System.Security.Policy;
+
+using Mono.Xml;
+
+namespace System.Security {
+
+       // Note: Using [SecurityPermissionAttribute] would be cool but triggers an error
+       // as you can't reference a custom security attribute from it's own assembly (CS0647)
+
+       public sealed class SecurityManager {
+
+               private static bool checkExecutionRights;
+               private static bool securityEnabled;
+               private static object _lockObject;
+               private static ArrayList _hierarchy;
+
+               static SecurityManager () 
+               {
+                       // lock(this) is bad
+                       // http://msdn.microsoft.com/library/en-us/dnaskdr/html/askgui06032003.asp?frame=true
+                       _lockObject = new object ();
+               }
+
+               private SecurityManager () {}
+
+               // properties
+
+               public static bool CheckExecutionRights {
+                       get { return checkExecutionRights; }
+                       set { 
+                               // throw a SecurityException if we don't have ControlPolicy permission
+                               new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+                               checkExecutionRights = value; 
+                       }
+               }
+
+               public static bool SecurityEnabled {
+                       get { return securityEnabled; }
+                       set { 
+                               // throw a SecurityException if we don't have ControlPolicy permission
+                               new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+                               securityEnabled = value; 
+                       }
+               }
+
+               // methods
+
+               [MonoTODO("Incomplete")]
+               public static bool IsGranted (IPermission perm)
+               {
+                       if (perm == null)
+                               return true;
+                       if (!securityEnabled)
+                               return true;
+                       return false;
+               }
+
+               public static PolicyLevel LoadPolicyLevelFromFile (string path, PolicyLevelType type)
+               {
+                       // throw a SecurityException if we don't have ControlPolicy permission
+                       new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
+
+                       PolicyLevel pl = null;
+                       try {
+                               pl = new PolicyLevel (type.ToString ());
+                               pl.LoadFromFile (path);
+                       }
+                       catch (Exception e) {
+                               throw new ArgumentException (Locale.GetText ("Invalid policy XML"), e);
+                       }
+                       return pl;
+               }
+
+               public static PolicyLevel LoadPolicyLevelFromString (string str, PolicyLevelType type)
+               {
+                       // throw a SecurityException if we don't have ControlPolicy permission
+                       new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+
+                       if (null == str)
+                               throw new ArgumentNullException ("str");
+
+                       PolicyLevel pl = null;
+                       try {
+                               pl = new PolicyLevel (type.ToString ());
+                               pl.LoadFromString (str);
+                       }
+                       catch (Exception e) {
+                               throw new ArgumentException (Locale.GetText ("Invalid policy XML"), e);
+                       }
+                       return pl;
+               }
+
+               [MonoTODO("InitializePolicyHierarchy isn't complete")]
+               public static IEnumerator PolicyHierarchy ()
+               {
+                       // throw a SecurityException if we don't have ControlPolicy permission
+                       new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+                       
+                       return Hierarchy;
+               }
+
+               [MonoTODO()]
+               public static PermissionSet ResolvePolicy (Evidence evidence)
+               {
+                       return null;
+               }
+
+               [MonoTODO()]
+               public static PermissionSet ResolvePolicy (Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, out PermissionSet denied)
+               {
+                       denied = null;
+                       return null;
+               }
+
+               [MonoTODO()]
+               public static IEnumerator ResolvePolicyGroups (Evidence evidence)
+               {
+                       return null;
+               }
+
+               [MonoTODO ("InternalSavePolicyLevel isn't complete")]
+               public static void SavePolicy () 
+               {
+                       // throw a SecurityException if we don't have ControlPolicy permission
+                       new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+
+                       IEnumerator e = Hierarchy;
+                       while (e.MoveNext ()) {
+                               PolicyLevel level = (e.Current as PolicyLevel);
+                               InternalSavePolicyLevel (level);
+                       }
+               }
+
+               [MonoTODO ("InternalSavePolicyLevel isn't complete")]
+               public static void SavePolicyLevel (PolicyLevel level) 
+               {
+                       // throw a SecurityException if we don't have ControlPolicy permission
+                       new SecurityPermission (SecurityPermissionFlag.ControlPolicy).Demand ();
+
+                       InternalSavePolicyLevel (level);
+               }
+
+               // internal stuff
+
+               internal static IEnumerator Hierarchy {
+                       get {
+                               if (_hierarchy == null) {
+                                       lock (_lockObject) {
+                                               InitializePolicyHierarchy ();
+                                       }
+                               }
+                               return _hierarchy.GetEnumerator ();
+                       }
+               }
+
+               internal static void InternalSavePolicyLevel (PolicyLevel level) 
+               {
+                       // without the security checks (to avoid checks in loops)
+               }
+
+               [MonoTODO ("Incomplete")]
+               internal static void InitializePolicyHierarchy ()
+               {
+                       ArrayList al = new ArrayList ();
+                       // minimum: Machine, Enterprise and User
+                       // FIXME: Incomplete
+                       al.Add (new PolicyLevel ("Enterprise"));
+                       al.Add (new PolicyLevel ("Machine"));
+                       al.Add (new PolicyLevel ("User"));
+                       _hierarchy = ArrayList.Synchronized (al);
+               }
+       }
+}