public static void Trace(TraceEventType type, int traceCode, string description, ServiceInfo info, Message message, int instanceID, Guid incomingTransactionID)
 {
     if (DiagnosticUtility.ShouldTrace(type))
     {
         WindowsIdentity messageIdentity = MessageUtil.GetMessageIdentity(message);
         Uri             from            = null;
         if (message.Headers.From != null)
         {
             from = message.Headers.From.Uri;
         }
         ComPlusInstanceCreationSuccessSchema extendedData = new ComPlusInstanceCreationSuccessSchema(info.AppID, info.Clsid, from, incomingTransactionID, messageIdentity.Name, instanceID);
         TraceUtility.TraceEvent(type, traceCode, System.ServiceModel.SR.GetString(description), extendedData, null, null, message);
     }
 }
 public static void Trace(TraceEventType type, int traceCode, string description, ServiceInfo info, Message message, Guid incomingTransactionID)
 {
     if (DiagnosticUtility.ShouldTrace(type))
     {
         WindowsIdentity callerIdentity = MessageUtil.GetMessageIdentity(message);
         Uri             from           = null;
         if (message.Headers.From != null)
         {
             from = message.Headers.From.Uri;
         }
         ComPlusInstanceCreationRequestSchema record =
             new ComPlusInstanceCreationRequestSchema(info.AppID, info.Clsid,
                                                      from, incomingTransactionID, callerIdentity.Name);
         TraceUtility.TraceEvent(type, traceCode, ServiceModelSR.GetString(description), record, null, null, message);
     }
 }
Exemplo n.º 3
0
        public object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
        {
            ComPlusServerSecurity       serverSecurity = null;
            WindowsImpersonationContext context        = null;
            object           obj3;
            bool             flag               = false;
            WindowsIdentity  clientIdentity     = null;
            Uri              from               = null;
            int              instanceID         = 0;
            string           action             = null;
            TransactionProxy proxy              = null;
            Transaction      messageTransaction = null;
            Guid             empty              = Guid.Empty;

            try
            {
                try
                {
                    clientIdentity = MessageUtil.GetMessageIdentity(message);
                    if (message.Headers.From != null)
                    {
                        from = message.Headers.From.Uri;
                    }
                    instanceID = instanceContext.GetServiceInstance(message).GetHashCode();
                    action     = message.Headers.Action;
                    ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x50018, "TraceCodeComIntegrationInvokingMethod", this.info, from, action, clientIdentity.Name, this.iid, instanceID, false);
                    if (this.info.CheckRoles && !this.comAuth.IsAuthorizedForOperation(clientIdentity))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
                    }
                    if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
                    {
                        serverSecurity = new ComPlusServerSecurity(clientIdentity, this.info.CheckRoles);
                    }
                    proxy = instanceContext.Extensions.Find <TransactionProxy>();
                    if (proxy != null)
                    {
                        messageTransaction = MessageUtil.GetMessageTransaction(message);
                        if (messageTransaction != null)
                        {
                            empty = messageTransaction.TransactionInformation.DistributedIdentifier;
                        }
                        try
                        {
                            if (messageTransaction == null)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
                            }
                            proxy.SetTransaction(messageTransaction);
                            ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001a, "TraceCodeComIntegrationInvokingMethodNewTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, empty);
                            goto Label_02DC;
                        }
                        catch (FaultException exception)
                        {
                            Transaction currentTransaction    = proxy.CurrentTransaction;
                            Guid        distributedIdentifier = Guid.Empty;
                            if (currentTransaction != null)
                            {
                                distributedIdentifier = currentTransaction.TransactionInformation.DistributedIdentifier;
                            }
                            string name = string.Empty;
                            if (clientIdentity != null)
                            {
                                name = clientIdentity.Name;
                            }
                            DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId)(-1073610725), new string[] { empty.ToString("B").ToUpperInvariant(), distributedIdentifier.ToString("B").ToUpperInvariant(), from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), name, exception.ToString() });
                            flag = true;
                            throw;
                        }
                    }
                    ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001b, "TraceCodeComIntegrationInvokingMethodContextTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, true);
Label_02DC:
                    if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
                    {
                        context = clientIdentity.Impersonate();
                    }
                    CorrelationState state = new CorrelationState(context, serverSecurity, from, action, clientIdentity.Name, instanceID);
                    context        = null;
                    serverSecurity = null;
                    obj3           = state;
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                    if (serverSecurity != null)
                    {
                        ((IDisposable)serverSecurity).Dispose();
                    }
                }
            }
            catch (Exception exception2)
            {
                if (!flag && DiagnosticUtility.ShouldTraceError)
                {
                    DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId)(-1073610727), new string[] { (from == null) ? string.Empty : from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString("B").ToUpperInvariant(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), clientIdentity.Name, exception2.ToString() });
                }
                throw;
            }
            return(obj3);
        }
Exemplo n.º 4
0
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            object result = null;
            Guid   incomingTransactionID = Guid.Empty;

            if (ContextUtil.IsInTransaction)
            {
                incomingTransactionID = ContextUtil.TransactionId;
            }
            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationRequest,
                                               SR.TraceCodeComIntegrationInstanceCreationRequest, this.info, message, incomingTransactionID);

            WindowsIdentity callerIdentity = null;

            callerIdentity = MessageUtil.GetMessageIdentity(message);
            WindowsImpersonationContext impersonateContext = null;

            try
            {
                try
                {
                    if (this.info.HostingMode ==
                        HostingMode.WebHostOutOfProcess)
                    {
                        if (SecurityUtils.IsAtleastImpersonationToken(new SafeCloseHandle(callerIdentity.Token, false)))
                        {
                            impersonateContext = callerIdentity.Impersonate();
                        }
                        else
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(SR.GetString(SR.BadImpersonationLevelForOutOfProcWas), HR.ERROR_BAD_IMPERSONATION_LEVEL));
                        }
                    }

                    CLSCTX clsctx = CLSCTX.SERVER;
                    if (PlatformSupportsBitness && (this.info.HostingMode ==
                                                    HostingMode.WebHostOutOfProcess))
                    {
                        if (this.info.Bitness == Bitness.Bitness32)
                        {
                            clsctx |= CLSCTX.ACTIVATE_32_BIT_SERVER;
                        }
                        else
                        {
                            clsctx |= CLSCTX.ACTIVATE_64_BIT_SERVER;
                        }
                    }

                    result = SafeNativeMethods.CoCreateInstance(
                        info.Clsid,
                        null,
                        clsctx,
                        IID_IUnknown);
                }
                finally
                {
                    if (impersonateContext != null)
                    {
                        impersonateContext.Undo();
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                Uri from = null;
                if (message.Headers.From != null)
                {
                    from = message.Headers.From.Uri;
                }

                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInstanceCreationError,
                                                    from == null ? string.Empty : from.ToString(),
                                                    this.info.AppID.ToString(),
                                                    this.info.Clsid.ToString(),
                                                    incomingTransactionID.ToString(),
                                                    callerIdentity.Name,
                                                    e.ToString());

                throw TraceUtility.ThrowHelperError(e, message);
            }


            TransactionProxy proxy = instanceContext.Extensions.Find <TransactionProxy>();

            if (proxy != null)
            {
                proxy.InstanceID = result.GetHashCode();
            }

            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationSuccess,
                                               SR.TraceCodeComIntegrationInstanceCreationSuccess, this.info, message, result.GetHashCode(), incomingTransactionID);
            return(result);
        }
        public object BeforeInvoke(
            InstanceContext instanceContext,
            IClientChannel channel,
            Message message)
        {
            ComPlusServerSecurity       serverSecurity     = null;
            WindowsImpersonationContext impersonateContext = null;
            bool             errorTraced           = false;
            WindowsIdentity  identity              = null;
            Uri              from                  = null;
            object           instance              = null;
            int              instanceID            = 0;
            string           action                = null;
            TransactionProxy proxy                 = null;
            Transaction      tx                    = null;
            Guid             incomingTransactionID = Guid.Empty;

            // The outer try block is to comply with FXCOP's WrapVulnerableFinallyClausesInOuterTry rule.
            try
            {
                try
                {
                    identity = MessageUtil.GetMessageIdentity(message);

                    if (message.Headers.From != null)
                    {
                        from = message.Headers.From.Uri;
                    }

                    instance   = instanceContext.GetServiceInstance(message);
                    instanceID = instance.GetHashCode();
                    action     = message.Headers.Action;



                    ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethod,
                                                 SR.TraceCodeComIntegrationInvokingMethod, this.info, from, action, identity.Name, iid, instanceID, false);

                    // Security
                    //

                    if (this.info.CheckRoles)
                    {
                        if (!this.comAuth.IsAuthorizedForOperation(identity))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
                        }
                    }

                    if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
                    {
                        // NOTE: This has the side effect of setting up
                        //       the COM server security thing, so be sure
                        //       to clear it with Dispose() eventually.
                        //
                        serverSecurity = new ComPlusServerSecurity(identity,
                                                                   this.info.CheckRoles);
                    }

                    // Transactions
                    //
                    proxy = instanceContext.Extensions.Find <TransactionProxy>();
                    if (proxy != null)
                    {
                        // This makes the Tx header Understood.
                        tx = MessageUtil.GetMessageTransaction(message);
                        if (tx != null)
                        {
                            incomingTransactionID = tx.TransactionInformation.DistributedIdentifier;
                        }
                        try
                        {
                            if (tx != null)
                            {
                                proxy.SetTransaction(tx);
                            }
                            else
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
                            }
                            ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethodNewTransaction,
                                                         SR.TraceCodeComIntegrationInvokingMethodNewTransaction, this.info, from, action, identity.Name, iid, instanceID, incomingTransactionID);
                        }
                        catch (FaultException e)
                        {
                            Transaction txProxy = proxy.CurrentTransaction;
                            Guid        currentTransactionID = Guid.Empty;
                            if (txProxy != null)
                            {
                                currentTransactionID = txProxy.TransactionInformation.DistributedIdentifier;
                            }

                            string identityName = String.Empty;

                            if (null != identity)
                            {
                                identityName = identity.Name;
                            }

                            DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                                (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                                                                (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInvokingMethodFailedMismatchedTransactions,
                                                                incomingTransactionID.ToString("B").ToUpperInvariant(),
                                                                currentTransactionID.ToString("B").ToUpperInvariant(),
                                                                from.ToString(),
                                                                this.info.AppID.ToString("B").ToUpperInvariant(),
                                                                this.info.Clsid.ToString("B").ToUpperInvariant(),
                                                                iid.ToString(),
                                                                action,
                                                                instanceID.ToString(CultureInfo.InvariantCulture),
                                                                System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture),
                                                                SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture),
                                                                identityName,
                                                                e.ToString());
                            errorTraced = true;
                            throw;
                        }
                    }
                    else
                    {
                        ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethodContextTransaction,
                                                     SR.TraceCodeComIntegrationInvokingMethodContextTransaction, this.info, from, action, identity.Name, iid, instanceID, true);
                    }

                    // Impersonation
                    //
                    if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
                    {
                        impersonateContext = identity.Impersonate();
                    }

                    CorrelationState correlationState;
                    correlationState = new CorrelationState(impersonateContext,
                                                            serverSecurity,
                                                            from,
                                                            action,
                                                            identity.Name,
                                                            instanceID);

                    impersonateContext = null;
                    serverSecurity     = null;


                    return(correlationState);
                }
                finally
                {
                    if (impersonateContext != null)
                    {
                        impersonateContext.Undo();
                    }

                    if (serverSecurity != null)
                    {
                        ((IDisposable)serverSecurity).Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                if (errorTraced == false)
                {
                    if (DiagnosticUtility.ShouldTraceError)
                    {
                        DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                            (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                                                            (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInvokingMethodFailed,
                                                            from == null ? string.Empty : from.ToString(),
                                                            this.info.AppID.ToString("B").ToUpperInvariant(),
                                                            this.info.Clsid.ToString("B").ToUpperInvariant(),
                                                            iid.ToString("B").ToUpperInvariant(),
                                                            action,
                                                            instanceID.ToString(CultureInfo.InvariantCulture),
                                                            System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture),
                                                            SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture),
                                                            identity.Name,
                                                            e.ToString());
                    }
                }
                throw;
            }
        }
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            object obj2  = null;
            Guid   empty = Guid.Empty;

            if (ContextUtil.IsInTransaction)
            {
                empty = ContextUtil.TransactionId;
            }
            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, 0x50012, "TraceCodeComIntegrationInstanceCreationRequest", this.info, message, empty);
            WindowsIdentity messageIdentity = null;

            messageIdentity = MessageUtil.GetMessageIdentity(message);
            WindowsImpersonationContext context = null;

            try
            {
                try
                {
                    if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
                    {
                        if (!System.ServiceModel.ComIntegration.SecurityUtils.IsAtleastImpersonationToken(new SafeCloseHandle(messageIdentity.Token, false)))
                        {
                            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(System.ServiceModel.SR.GetString("BadImpersonationLevelForOutOfProcWas"), HR.ERROR_BAD_IMPERSONATION_LEVEL));
                        }
                        context = messageIdentity.Impersonate();
                    }
                    CLSCTX sERVER = CLSCTX.SERVER;
                    if (PlatformSupportsBitness && (this.info.HostingMode == HostingMode.WebHostOutOfProcess))
                    {
                        if (this.info.Bitness == Bitness.Bitness32)
                        {
                            sERVER |= CLSCTX.ACTIVATE_32_BIT_SERVER;
                        }
                        else
                        {
                            sERVER |= CLSCTX.ACTIVATE_64_BIT_SERVER;
                        }
                    }
                    obj2 = System.ServiceModel.ComIntegration.SafeNativeMethods.CoCreateInstance(this.info.Clsid, null, sERVER, IID_IUnknown);
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                Uri uri = null;
                if (message.Headers.From != null)
                {
                    uri = message.Headers.From.Uri;
                }
                System.ServiceModel.DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId)(-1073610726), new string[] { (uri == null) ? string.Empty : uri.ToString(), this.info.AppID.ToString(), this.info.Clsid.ToString(), empty.ToString(), messageIdentity.Name, exception.ToString() });
                throw TraceUtility.ThrowHelperError(exception, message);
            }
            System.ServiceModel.ComIntegration.TransactionProxy proxy = instanceContext.Extensions.Find <System.ServiceModel.ComIntegration.TransactionProxy>();
            if (proxy != null)
            {
                proxy.InstanceID = obj2.GetHashCode();
            }
            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, 0x50013, "TraceCodeComIntegrationInstanceCreationSuccess", this.info, message, obj2.GetHashCode(), empty);
            return(obj2);
        }