public void CheckAssemblySCValidity(Assembly asm)
        {
            Type[]               types    = null;
            bool                 flag     = true;
            ArrayList            list     = null;
            RegistrationServices services = new RegistrationServices();

            try
            {
                types = asm.GetTypes();
            }
            catch (ReflectionTypeLoadException exception)
            {
                types = exception.Types;
            }
            foreach (Type type in types)
            {
                if (((null != type) && type.IsClass) && type.IsSubclassOf(typeof(ServicedComponent)))
                {
                    if (!services.TypeRequiresRegistration(type) && !type.IsAbstract)
                    {
                        flag = false;
                        if (list == null)
                        {
                            list = new ArrayList();
                        }
                        RegistrationErrorInfo info = new RegistrationErrorInfo(null, null, type.ToString(), -2147467259);
                        list.Add(info);
                    }
                    ClassInterfaceType classInterfaceType = ServicedComponentInfo.GetClassInterfaceType(type);
                    foreach (MethodInfo info2 in type.GetMethods())
                    {
                        if (ReflectionCache.ConvertToInterfaceMI(info2) == null)
                        {
                            if (ServicedComponentInfo.HasSpecialMethodAttributes(info2))
                            {
                                this.ReportWarning(Resource.FormatString("Reg_NoClassInterfaceSecure", type.FullName, info2.Name));
                            }
                            if ((classInterfaceType == ClassInterfaceType.AutoDispatch) && ServicedComponentInfo.IsMethodAutoDone(info2))
                            {
                                this.ReportWarning(Resource.FormatString("Reg_NoClassInterface", type.FullName, info2.Name));
                            }
                        }
                    }
                }
            }
            if (!flag)
            {
                RegistrationErrorInfo[] errorInfo = (RegistrationErrorInfo[])list.ToArray(typeof(RegistrationErrorInfo));
                throw new RegistrationException(Resource.FormatString("Reg_InvalidServicedComponents"), errorInfo);
            }
        }
        private bool SendMethodCall(IMessage req)
        {
            bool flag = false;

            if (this._tracker != null)
            {
                IntPtr zero = IntPtr.Zero;
                try
                {
                    IMethodCallMessage message = req as IMethodCallMessage;
                    if (!this.IsRealCall(message.MethodBase))
                    {
                        return(false);
                    }
                    if (Util.ExtendedLifetime)
                    {
                        zero = this.SupportsInterface(ref _s_IID_IManagedObjectInfo);
                    }
                    else
                    {
                        zero = this.GetOuterIUnknown();
                    }
                    MethodBase method = ReflectionCache.ConvertToInterfaceMI(message.MethodBase) as MethodBase;
                    if (method != null)
                    {
                        this._tracker.SendMethodCall(zero, method);
                        flag = true;
                    }
                }
                catch (Exception exception)
                {
                    if ((exception is NullReferenceException) || (exception is SEHException))
                    {
                        throw;
                    }
                    return(flag);
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        Marshal.Release(zero);
                    }
                }
            }
            return(flag);
        }
示例#3
0
        private void CheckMethodAccess(IMessage request)
        {
            MethodBase     mi      = null;
            MethodBase     m       = null;
            IMethodMessage message = request as IMethodMessage;

            if (message == null)
            {
                throw new UnauthorizedAccessException();
            }
            mi = message.MethodBase;
            m  = ReflectionCache.ConvertToClassMI(base.GetType(), mi) as MethodBase;
            if (m == null)
            {
                throw new UnauthorizedAccessException();
            }
            if (ServicedComponentInfo.HasSpecialMethodAttributes(m))
            {
                throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
            }
            if ((!mi.IsPublic || mi.IsStatic) && !IsMethodAllowedRemotely(mi))
            {
                throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
            }
            Type declaringType = mi.DeclaringType;

            if (!declaringType.IsPublic && !declaringType.IsNestedPublic)
            {
                throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
            }
            for (declaringType = mi.DeclaringType.DeclaringType; declaringType != null; declaringType = declaringType.DeclaringType)
            {
                if (!declaringType.IsPublic && !declaringType.IsNestedPublic)
                {
                    throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
                }
            }
        }
示例#4
0
        // Implement Invoke
        public override IMessage Invoke(IMessage reqMsg)
        {
            AssertValid();
            IMessage retMsg = null;

            // CHECK FOR CTOR
            if (reqMsg is IConstructionCallMessage)
            {
                DBG.Info(DBG.SC, "RSCP: Short circuiting constructor call.");

                if (((IConstructionCallMessage)reqMsg).ArgCount > 0)
                {
                    throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_ConstructorArguments"));
                }

                // WE don't need dispatch the constructor message, the default .ctor is called
                // by CoCI
                // Create the return message
                MarshalByRefObject retObj = (MarshalByRefObject)GetTransparentProxy();
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)reqMsg, retObj));
            }

            // NON CTOR MESSAGE
            MethodBase mb = ((IMethodMessage)reqMsg).MethodBase;
            MemberInfo m  = (MemberInfo)mb;

            DBG.Assert(m != null, "member info should not be null");

            if (mb == _getTypeMethod)
            {
                DBG.Info(DBG.SC, "RSCP: Hijacking call to GetType");
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(ProxiedType, null, 0, mcm.LogicalCallContext, mcm));
            }

            if (mb == _getHashCodeMethod)
            {
                int hashCode = this.GetHashCode();
                DBG.Info(DBG.SC, "RSCP: Hijacking call to GetHashCode, returning " + hashCode);
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(hashCode, null, 0, mcm.LogicalCallContext, mcm));
            }

            DBG.Info(DBG.SC, "RSCP: Delivering call to " + mb.Name);

            // convert to class member info
            MemberInfo clsMemInfo = ReflectionCache.ConvertToClassMI(ProxiedType, m);

            DBG.Assert(!clsMemInfo.ReflectedType.IsInterface,
                       "Failed to convert interface member info to class member info");

            // dispatch the call
#if _DEBUG
            if (clsMemInfo != m)
            {
                DBG.Info(DBG.SC, "RSCP: converted interface to class call: " + clsMemInfo.Name);
            }
#endif

            try
            {
                int iMethodInfo;
                // check for rolebased security
                if (_fUseIntfDispatch ||
                    (((iMethodInfo = ServicedComponentInfo.MICachedLookup(clsMemInfo)) & ServicedComponentInfo.MI_HASSPECIALATTRIBUTES) != 0) ||
                    ((iMethodInfo & ServicedComponentInfo.MI_EXECUTEMESSAGEVALID) != 0)
                    )
                {
                    // role based security implies we dispatch through an interface
                    MemberInfo intfMemInfo = ReflectionCache.ConvertToInterfaceMI(m);
                    if (intfMemInfo == null)
                    {
                        throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
                    }
                    //retMsg = EnterpriseServicesHelper.DispatchRemoteCall(reqMsg, intfMemInfo, _server);
                    MethodCallMessageWrapperEx msgex = new MethodCallMessageWrapperEx((IMethodCallMessage)reqMsg, (MethodBase)intfMemInfo);
                    retMsg = RemotingServices.ExecuteMessage((MarshalByRefObject)_server, (IMethodCallMessage)msgex);
                }
                else
                {
                    // check for AutoDone
                    bool fAutoDone = (iMethodInfo & ServicedComponentInfo.MI_AUTODONE) != 0;

                    String s = ComponentServices.ConvertToString(reqMsg);

                    IRemoteDispatch iremDisp = (IRemoteDispatch)_server;

                    String sret;
                    if (fAutoDone)
                    {
                        sret = iremDisp.RemoteDispatchAutoDone(s);
                    }
                    else
                    {
                        sret = iremDisp.RemoteDispatchNotAutoDone(s);
                    }
                    retMsg = ComponentServices.ConvertToReturnMessage(sret, reqMsg);
                }
            }
            catch (COMException e)
            {
                if (!(e.ErrorCode == Util.CONTEXT_E_ABORTED || e.ErrorCode == Util.CONTEXT_E_ABORTING))
                {
                    throw;
                }
                if (IsDisposeRequest(reqMsg))
                {
                    IMethodCallMessage mcm = reqMsg as IMethodCallMessage;
                    retMsg = new ReturnMessage(null, null, 0, mcm.LogicalCallContext, mcm);
                }
                else
                {
                    throw;
                }
            }

            // if disposing, we need to release this side of the world.
            if (IsDisposeRequest(reqMsg))
            {
                Dispose(true);
            }

            return(retMsg);
        }
示例#5
0
        private void CheckMethodAccess(IMessage request)
        {
            MethodBase reqmethod  = null; // Method from the request
            MethodBase implmethod = null; // The implementation we will dispatch on.

            IMethodMessage call = request as IMethodMessage;

            if (call == null)
            {
                throw new UnauthorizedAccessException();
            }

            reqmethod = call.MethodBase;

            // Make sure we investigate the implementation, not the interface
            // for attributes (such as SecureMethod)
            implmethod = ReflectionCache.ConvertToClassMI(GetType(), reqmethod) as MethodBase;
            if (implmethod == null)
            {
                throw new UnauthorizedAccessException();
            }

            // Check implementation for dispatch attributes
            if (ServicedComponentInfo.HasSpecialMethodAttributes(implmethod))
            {
                throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
            }

            // Verify the method is not private, internal, or static.  Use
            // the method that's being requested here, in case it's an
            // explicit interface implementation (they're all private).
            if (!reqmethod.IsPublic || reqmethod.IsStatic)
            {
                // If this is a special method (such as the FieldGetter or Setter, thne
                // go ahead and let it through.
                if (!IsMethodAllowedRemotely(reqmethod))
                {
                    throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
                }
            }

            // Make sure that the type we're invoking on is public!  This
            // covers invokes on public methods of internal interfaces.
            Type reqtype = reqmethod.DeclaringType;

            if (!reqtype.IsPublic && !reqtype.IsNestedPublic)
            {
                throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
            }

            // Deal with nested types!  Get the declaring type of the method,
            // and then get the outer type that declared that.  It'll be
            // non-null if this is a nested type.
            reqtype = reqmethod.DeclaringType.DeclaringType;
            while (reqtype != null)
            {
                if (!reqtype.IsPublic && !reqtype.IsNestedPublic)
                {
                    throw new UnauthorizedAccessException(Resource.FormatString("ServicedComponentException_SecurityNoPrivateAccess"));
                }

                reqtype = reqtype.DeclaringType;
            }
        }
        public override IMessage Invoke(IMessage reqMsg)
        {
            this.AssertValid();
            IMessage message = null;

            if (reqMsg is IConstructionCallMessage)
            {
                if (((IConstructionCallMessage)reqMsg).ArgCount > 0)
                {
                    throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_ConstructorArguments"));
                }
                MarshalByRefObject transparentProxy = (MarshalByRefObject)this.GetTransparentProxy();
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)reqMsg, transparentProxy));
            }
            MethodBase methodBase = ((IMethodMessage)reqMsg).MethodBase;
            MemberInfo mi         = methodBase;

            if (methodBase == _getTypeMethod)
            {
                IMethodCallMessage mcm = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(this.ProxiedType, null, 0, mcm.LogicalCallContext, mcm));
            }
            if (methodBase == _getHashCodeMethod)
            {
                int hashCode = this.GetHashCode();
                IMethodCallMessage message3 = (IMethodCallMessage)reqMsg;
                return(new ReturnMessage(hashCode, null, 0, message3.LogicalCallContext, message3));
            }
            if (methodBase == _isInstanceOfTypeMethod)
            {
                IMethodCallMessage message4 = (IMethodCallMessage)reqMsg;
                Type inArg = (Type)message4.GetInArg(0);
                return(new ReturnMessage(inArg.IsInstanceOfType(this.ProxiedType), null, 0, message4.LogicalCallContext, message4));
            }
            MemberInfo m = ReflectionCache.ConvertToClassMI(this.ProxiedType, mi);

            try
            {
                int num2;
                if ((this._fUseIntfDispatch || (((num2 = ServicedComponentInfo.MICachedLookup(m)) & 4) != 0)) || ((num2 & 8) != 0))
                {
                    MemberInfo info3 = ReflectionCache.ConvertToInterfaceMI(mi);
                    if (info3 == null)
                    {
                        throw new ServicedComponentException(Resource.FormatString("ServicedComponentException_SecurityMapping"));
                    }
                    MethodCallMessageWrapperEx ex = new MethodCallMessageWrapperEx((IMethodCallMessage)reqMsg, (MethodBase)info3);
                    message = RemotingServices.ExecuteMessage((MarshalByRefObject)this._server, ex);
                }
                else
                {
                    string          str2;
                    bool            flag     = (num2 & 2) != 0;
                    string          s        = ComponentServices.ConvertToString(reqMsg);
                    IRemoteDispatch dispatch = (IRemoteDispatch)this._server;
                    if (flag)
                    {
                        str2 = dispatch.RemoteDispatchAutoDone(s);
                    }
                    else
                    {
                        str2 = dispatch.RemoteDispatchNotAutoDone(s);
                    }
                    message = ComponentServices.ConvertToReturnMessage(str2, reqMsg);
                }
            }
            catch (COMException exception)
            {
                if ((exception.ErrorCode != -2147164158) && (exception.ErrorCode != -2147164157))
                {
                    throw;
                }
                if (!this.IsDisposeRequest(reqMsg))
                {
                    throw;
                }
                IMethodCallMessage message5 = reqMsg as IMethodCallMessage;
                message = new ReturnMessage(null, null, 0, message5.LogicalCallContext, message5);
            }
            if (this.IsDisposeRequest(reqMsg))
            {
                this.Dispose(true);
            }
            return(message);
        }