Пример #1
0
        private RPC_CALL_ATTRIBUTES_V2 GetCallInfo()
        {
            if (_callAttrs.Version != 0)
            {
                return(_callAttrs);
            }
            RPC_CALL_ATTRIBUTES_V2 attrs = new RPC_CALL_ATTRIBUTES_V2();

            attrs.Version = 2;
            attrs.Flags   = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED;
            RpcError err = RpcServerInqCallAttributes(_clientHandle, ref attrs);

            if (err == RpcError.RPC_S_INVALID_ARG) //may not support v2 on early edditions of XP/SP3
            {
                attrs.Version = 1;
                err           = RpcServerInqCallAttributes(_clientHandle, ref attrs);
            }

            if (err == RpcError.RPC_S_OK)
            {
                _callAttrs       = attrs;
                _isAuthenticated = false;
                attrs.Flags      = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_IS_CLIENT_LOCAL |
                                   RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED;
                if ((err = RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RpcError.RPC_S_OK)
                {
                    _callAttrs.IsClientLocal = attrs.IsClientLocal;

                    if (_callAttrs.ProtocolSequence == RpcProtoseqType.LRPC)
                    {
                        attrs.Flags = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CLIENT_PID;
                        if ((err = RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RpcError.RPC_S_OK)
                        {
                            _callAttrs.ClientPID = attrs.ClientPID;
                        }
                    }
                }

                if (_callAttrs.ProtocolSequence != RpcProtoseqType.LRPC)
                {
                    using (Ptr <byte[]> callerAddress = new Ptr <byte[]>(new byte[1024]))
                    {
                        RPC_CALL_LOCAL_ADDRESS_V1 localAddress = new RPC_CALL_LOCAL_ADDRESS_V1();
                        localAddress.Version       = 1;
                        localAddress.Buffer        = callerAddress.Handle;
                        localAddress.BufferSize    = 1024;
                        localAddress.AddressFormat = RpcLocalAddressFormat.Invalid;
                        _callAttrs = attrs;

                        using (
                            Ptr <RPC_CALL_LOCAL_ADDRESS_V1> callerAddressv1 =
                                new Ptr <RPC_CALL_LOCAL_ADDRESS_V1>(localAddress))
                        {
                            attrs.CallLocalAddress = callerAddressv1.Handle;
                            attrs.Flags            = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CALL_LOCAL_ADDRESS |
                                                     RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED;
                            if ((err = RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RpcError.RPC_S_OK)
                            {
                                _clientAddress = new byte[callerAddressv1.Data.BufferSize];
                                Array.Copy(callerAddress.Data, _clientAddress, _clientAddress.Length);
                            }
                        }
                    }
                }

                using (Ptr <byte[]> clientPrincipal = new Ptr <byte[]>(new byte[1024]))
                {
                    attrs.ClientPrincipalName             = clientPrincipal.Handle;
                    attrs.ClientPrincipalNameBufferLength = 1024;
                    attrs.Flags = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CLIENT_PRINCIPAL_NAME;
                    if ((err = RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RpcError.RPC_S_OK)
                    {
                        _clientPrincipalName = Marshal.PtrToStringUni(clientPrincipal.Handle);

                        if (!String.IsNullOrEmpty(_clientPrincipalName))
                        {
                            _isAuthenticated = true;
                            //On Windows XP this only returns a value on LRPC so we know they are local
                            if (attrs.Version == 1)
                            {
                                _callAttrs.IsClientLocal = RpcCallClientLocality.Local;
                            }
                        }
                    }
                }
            }
            else
            {
                Log.Warning("RpcServerInqCallAttributes error {0} = {1}", err, new RpcException(err).Message);
            }


            return(_callAttrs);
        }