예제 #1
0
        internal void ServiceRequest(Object state)
        {
            IpcServerHandler ipcServerHandler = (IpcServerHandler)state;

            // Read the headers from the stream, using the header size in the message
            ITransportHeaders headers = ipcServerHandler.ReadHeaders();

            // Get the request Stream
            Stream requestStream = ipcServerHandler.GetRequestStream();

            // customErrors should be disabled, since we are on the same machine
            headers["__CustomErrorsEnabled"] = false;

            // process request
            ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();

            sinkStack.Push(this, ipcServerHandler);

            IMessage          responseMessage = null;
            ITransportHeaders responseHeaders = null;
            Stream            responseStream  = null;
            WindowsIdentity   identity        = null;
            IPrincipal        oldPrincipal    = null;
            bool             impersonated     = false;
            bool             principalChanged = false;
            ServerProcessing processing       = ServerProcessing.Complete;

            try{
                if (_secure)
                {
                    IpcPort port = ipcServerHandler.Port;
                    port.ImpersonateClient();
                    oldPrincipal     = Thread.CurrentPrincipal;
                    principalChanged = true;
                    impersonated     = true;
                    identity         = WindowsIdentity.GetCurrent();
                    // If the authentication mode is to identify callers only revert the impersonation immediately
                    if (!_impersonate)
                    {
                        NativePipe.RevertToSelf();
                        Thread.CurrentPrincipal = new GenericPrincipal(identity, null);
                        impersonated            = false;
                    }
                    else
                    {
                        if (identity.ImpersonationLevel == TokenImpersonationLevel.Impersonation ||
                            identity.ImpersonationLevel == TokenImpersonationLevel.Delegation)
                        {
                            // Set the current principal
                            Thread.CurrentPrincipal = new WindowsPrincipal(identity);
                        }
                        else
                        {
                            throw new RemotingException(CoreChannel.GetResourceString(
                                                            "Remoting_Ipc_TokenImpersonationFailure"));
                        }
                    }
                }

                processing =
                    _nextSink.ProcessMessage(sinkStack, null, headers, requestStream,
                                             out responseMessage,
                                             out responseHeaders, out responseStream);
            }
            catch (Exception e) {
                ipcServerHandler.CloseOnFatalError(e);
            }
            finally{
                // Revert the principal if we had changed the principal
                if (principalChanged)
                {
                    Thread.CurrentPrincipal = oldPrincipal;
                }
                // Revert the impersonation if we had impersonated
                if (impersonated)
                {
                    NativePipe.RevertToSelf();
                    impersonated = false;
                }
            }

            // handle response
            switch (processing)
            {
            case ServerProcessing.Complete:
            {
                // Send the response. Call completed synchronously.
                sinkStack.Pop(this);
                // Send the response back to the client
                ipcServerHandler.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.Complete

            case ServerProcessing.OneWay:
            {
                // No response needed, but the following method will make sure that
                //   we send at least a skeleton reply if the incoming request was
                //   not marked OneWayRequest (client/server metadata could be out of
                //   [....]).
                ipcServerHandler.SendResponse(responseHeaders, responseStream);
                break;
            } // case ServerProcessing.OneWay

            case ServerProcessing.Async:
            {
                sinkStack.StoreAndDispatch(this, ipcServerHandler);
                break;
            } // case ServerProcessing.Async
            } // switch (processing)

            // Start waiting for the next request
            if (processing != ServerProcessing.Async)
            {
                ipcServerHandler.BeginReadMessage();
            }
        } // ServiceRequest