Merge pull request #390 from baulig/master
[mono.git] / mono / metadata / security-manager.c
index 9b76c939a70a8befa8d61e3202722fdb150f87bb..cab10abf6f11d9834e9c628c375e6f08eb8aa00d 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Sebastien Pouliot  <sebastien@ximian.com>
  *
- * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+ * Copyright 2005-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include "security-manager.h"
@@ -16,10 +16,23 @@ static MonoSecurityManager secman;
 static MonoBoolean mono_security_manager_activated = FALSE;
 static MonoBoolean mono_security_manager_enabled = TRUE;
 static MonoBoolean mono_security_manager_execution = TRUE;
+static MonoSecurityMode mono_security_mode = MONO_SECURITY_MODE_NONE;
 
 
 /* Public stuff */
 
+void
+mono_security_set_mode (MonoSecurityMode mode)
+{
+       mono_security_mode = mode;
+}
+
+MonoSecurityMode
+mono_security_get_mode (void)
+{
+       return mono_security_mode;
+}
+
 MonoSecurityManager*
 mono_security_manager_get_methods (void)
 {
@@ -47,7 +60,7 @@ mono_security_manager_get_methods (void)
        g_assert (secman.demandunmanaged);
 
        secman.inheritancedemand = mono_class_get_method_from_name (secman.securitymanager,
-               "InheritanceDemand", 2);        
+               "InheritanceDemand", 3);        
        g_assert (secman.inheritancedemand);
 
        secman.inheritsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
@@ -67,7 +80,7 @@ mono_security_manager_get_methods (void)
        g_assert (secman.linkdemandfulltrust);
 
        secman.linkdemandsecurityexception = mono_class_get_method_from_name (secman.securitymanager,
-               "LinkDemandSecurityException", 3);
+               "LinkDemandSecurityException", 2);
        g_assert (secman.linkdemandsecurityexception);
 
        secman.allowpartiallytrustedcallers = mono_class_from_name (mono_defaults.corlib, "System.Security", 
@@ -89,10 +102,11 @@ mono_secman_inheritance_check (MonoClass *klass, MonoDeclSecurityActions *demand
        MonoAssembly *assembly = mono_image_get_assembly (klass->image);
        MonoReflectionAssembly *refass = mono_assembly_get_object (domain, assembly);
        MonoObject *res;
-       gpointer args [2];
+       gpointer args [3];
 
-       args [0] = refass;
-       args [1] = demands;
+       args [0] = domain->domain;
+       args [1] = refass;
+       args [2] = demands;
 
        res = mono_runtime_invoke (secman->inheritancedemand, NULL, args, NULL);
        return (*(MonoBoolean *) mono_object_unbox (res));
@@ -117,8 +131,7 @@ mono_secman_inheritancedemand_class (MonoClass *klass, MonoClass *parent)
                /* If so check the demands on the klass (inheritor) */
                if (!mono_secman_inheritance_check (klass, &demands)) {
                        /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
-                       klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
-                       klass->exception_data = NULL;
+                       mono_class_set_failure (klass, MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND, NULL);
                }
        }
 }
@@ -142,8 +155,7 @@ mono_secman_inheritancedemand_method (MonoMethod *override, MonoMethod *base)
                /* If so check the demands on the overriding method */
                if (!mono_secman_inheritance_check (override->klass, &demands)) {
                        /* Keep flags in MonoClass to be able to throw a SecurityException later (if required) */
-                       override->klass->exception_type = MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND;
-                       override->klass->exception_data = base;
+                       mono_class_set_failure (override->klass, MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND, base);
                }
        }
 }
@@ -222,8 +234,12 @@ mono_get_context_capture_method (void)
 MonoBoolean
 ves_icall_System_Security_SecurityManager_get_SecurityEnabled (void)
 {
-       if (!mono_security_manager_activated)
-               return FALSE;
+       if (!mono_security_manager_activated) {
+               /* SecurityManager is internal for Moonlight and SecurityEnabled is used to know if CoreCLR is active
+                * (e.g. plugin executing in the browser) or not (e.g. smcs compiling source code with corlib 2.1)
+                */
+               return (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR);
+       }
        return mono_security_manager_enabled;
 }
 
@@ -258,7 +274,7 @@ ves_icall_System_Security_SecurityManager_GetLinkDemandSecurity (MonoReflectionM
 {
        MonoMethod *method = m->method;
        /* we want the original as the wrapper is "free" of the security informations */
-       if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
+       if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
                method = mono_marshal_method_from_wrapper (method);
        }