### ms-tsts_tspubrpc.idl
"""
RPC definitions for the following interfaces:
- TermSrvSession (v1.0): 484809d6-4239-471b-b5bc-61df8c23ac48
- TermSrvNotification (v1.0): 11899a43-2b68-4a76-92e3-a3d6ad8c26ce
- TermSrvEnumeration (v1.0): 88143fd0-c28d-4b2b-8fef-8d882f6a9390
This file is auto-generated by midl-to-scapy, do not modify.
"""

from enum import IntEnum
import uuid

from scapy.fields import StrFixedLenField, StrFixedLenFieldUtf16
from scapy.layers.dcerpc import (
    NDRPacket,
    DceRpcOp,
    NDRConfFieldListField,
    NDRConfPacketListField,
    NDRConfStrLenField,
    NDRConfVarStrNullField,
    NDRConfVarStrNullFieldUtf16,
    NDRContextHandle,
    NDRFullPointerField,
    NDRInt3264EnumField,
    NDRIntField,
    NDRPacketField,
    NDRShortField,
    NDRSignedIntField,
    NDRSignedLongField,
    NDRUnionField,
    register_dcerpc_interface,
)


class RpcOpenSession_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("SessionId", 0)]


class RpcOpenSession_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phSession", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


class RpcCloseSession_Request(NDRPacket):
    fields_desc = [NDRPacketField("phSession", NDRContextHandle(), NDRContextHandle)]


class RpcCloseSession_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phSession", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


class RpcConnect_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle),
        NDRSignedIntField("TargetSessionId", 0),
        NDRConfVarStrNullFieldUtf16("szPassword", ""),
    ]


class RpcConnect_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcDisconnect_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcDisconnect_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcLogoff_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcLogoff_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcGetUserName_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcGetUserName_Response(NDRPacket):
    fields_desc = [
        NDRFullPointerField(NDRConfVarStrNullFieldUtf16("pszUserName", "")),
        NDRFullPointerField(NDRConfVarStrNullFieldUtf16("pszDomain", "")),
        NDRIntField("status", 0),
    ]


class RpcGetTerminalName_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcGetTerminalName_Response(NDRPacket):
    fields_desc = [
        NDRFullPointerField(NDRConfVarStrNullFieldUtf16("pszTerminalName", "")),
        NDRIntField("status", 0),
    ]


class RpcGetState_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcGetState_Response(NDRPacket):
    fields_desc = [NDRSignedIntField("plState", 0), NDRIntField("status", 0)]


class RpcIsSessionDesktopLocked_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcIsSessionDesktopLocked_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcShowMessageBox_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle),
        NDRConfVarStrNullFieldUtf16("szTitle", ""),
        NDRConfVarStrNullFieldUtf16("szMessage", ""),
        NDRIntField("ulStyle", 0),
        NDRIntField("ulTimeout", 0),
        NDRSignedIntField("bDoNotWait", 0),
    ]


class RpcShowMessageBox_Response(NDRPacket):
    fields_desc = [NDRIntField("pulResponse", 0), NDRIntField("status", 0)]


class RpcGetTimes_Request(NDRPacket):
    fields_desc = [NDRPacketField("hSession", NDRContextHandle(), NDRContextHandle)]


class RpcGetTimes_Response(NDRPacket):
    fields_desc = [
        NDRSignedLongField("pConnectTime", 0),
        NDRSignedLongField("pDisconnectTime", 0),
        NDRSignedLongField("pLogonTime", 0),
        NDRIntField("status", 0),
    ]


class TS_COUNTER_HEADER(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [NDRIntField("dwCounterID", 0), NDRIntField("bResult", 0)]


class LARGE_INTEGER(NDRPacket):
    ALIGNMENT = (8, 8)
    fields_desc = [NDRSignedLongField("QuadPart", 0)]


class PTS_COUNTER(NDRPacket):
    ALIGNMENT = (8, 8)
    fields_desc = [
        NDRPacketField("counterHead", TS_COUNTER_HEADER(), TS_COUNTER_HEADER),
        NDRIntField("dwValue", 0),
        NDRPacketField("startTime", LARGE_INTEGER(), LARGE_INTEGER),
    ]


class RpcGetSessionCounters_Request(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "pCounter", [PTS_COUNTER()], PTS_COUNTER, size_is=lambda pkt: pkt.uEntries
        ),
        NDRIntField("uEntries", None, size_of="pCounter"),
    ]


class RpcGetSessionCounters_Response(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "pCounter", [PTS_COUNTER()], PTS_COUNTER, size_is=lambda pkt: pkt.uEntries
        ),
        NDRIntField("status", 0),
    ]


class PLSMSESSIONINFORMATION(NDRPacket):
    ALIGNMENT = (8, 8)
    fields_desc = [
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszUserName", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszDomain", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszTerminalName", ""), deferred=True
        ),
        NDRSignedIntField("SessionState", 0),
        NDRSignedIntField("DesktopLocked", 0),
        NDRSignedLongField("ConnectTime", 0),
        NDRSignedLongField("DisconnectTime", 0),
        NDRSignedLongField("LogonTime", 0),
    ]


class RpcGetSessionInformation_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("SessionId", 0)]


class RpcGetSessionInformation_Response(NDRPacket):
    fields_desc = [
        NDRPacketField(
            "pSessionInfo", PLSMSESSIONINFORMATION(), PLSMSESSIONINFORMATION
        ),
        NDRIntField("status", 0),
    ]


class RpcGetLoggedOnCount_Request(NDRPacket):
    fields_desc = []


class RpcGetLoggedOnCount_Response(NDRPacket):
    fields_desc = [
        NDRIntField("pUserSessions", 0),
        NDRIntField("pDeviceSessions", 0),
        NDRIntField("status", 0),
    ]


class RpcGetSessionType_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("SessionId", 0)]


class RpcGetSessionType_Response(NDRPacket):
    fields_desc = [NDRIntField("pSessionType", 0), NDRIntField("status", 0)]


class LSM_SESSIONINFO_EX_LEVEL1(NDRPacket):
    ALIGNMENT = (8, 8)
    fields_desc = [
        NDRSignedIntField("SessionState", 0),
        NDRSignedIntField("SessionFlags", 0),
        StrFixedLenFieldUtf16("SessionName", "", length=33 * 2),
        StrFixedLenFieldUtf16("DomainName", "", length=18 * 2),
        StrFixedLenFieldUtf16("UserName", "", length=21 * 2),
        NDRSignedLongField("ConnectTime", 0),
        NDRSignedLongField("DisconnectTime", 0),
        NDRSignedLongField("LogonTime", 0),
        NDRSignedLongField("LastInputTime", 0),
        NDRIntField("ProtocolDataSize", None, size_of="ProtocolData"),
        NDRFullPointerField(
            NDRConfStrLenField(
                "ProtocolData", "", size_is=lambda pkt: pkt.ProtocolDataSize
            ),
            deferred=True,
        ),
    ]


class PLSMSESSIONINFORMATION_EX(NDRPacket):
    ALIGNMENT = (8, 8)
    fields_desc = [
        NDRIntField("Level", 0),
        NDRUnionField(
            [
                (
                    NDRPacketField(
                        "Data", LSM_SESSIONINFO_EX_LEVEL1(), LSM_SESSIONINFO_EX_LEVEL1
                    ),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 1),
                        (lambda _, val: val.tag == 1),
                    ),
                )
            ],
            StrFixedLenField("Data", "", length=0),
            align=(4, 8),
            switch_fmt=("L", "L"),
        ),
    ]


class RpcGetSessionInformationEx_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("SessionId", 0), NDRIntField("Level", 0)]


class RpcGetSessionInformationEx_Response(NDRPacket):
    fields_desc = [
        NDRPacketField(
            "LSMSessionInfoExPtr",
            PLSMSESSIONINFORMATION_EX(),
            PLSMSESSIONINFORMATION_EX,
        ),
        NDRIntField("status", 0),
    ]


TERMSRVSESSION_OPNUMS = {
    0: DceRpcOp(RpcOpenSession_Request, RpcOpenSession_Response),
    1: DceRpcOp(RpcCloseSession_Request, RpcCloseSession_Response),
    2: DceRpcOp(RpcConnect_Request, RpcConnect_Response),
    3: DceRpcOp(RpcDisconnect_Request, RpcDisconnect_Response),
    4: DceRpcOp(RpcLogoff_Request, RpcLogoff_Response),
    5: DceRpcOp(RpcGetUserName_Request, RpcGetUserName_Response),
    6: DceRpcOp(RpcGetTerminalName_Request, RpcGetTerminalName_Response),
    7: DceRpcOp(RpcGetState_Request, RpcGetState_Response),
    8: DceRpcOp(RpcIsSessionDesktopLocked_Request, RpcIsSessionDesktopLocked_Response),
    9: DceRpcOp(RpcShowMessageBox_Request, RpcShowMessageBox_Response),
    10: DceRpcOp(RpcGetTimes_Request, RpcGetTimes_Response),
    11: DceRpcOp(RpcGetSessionCounters_Request, RpcGetSessionCounters_Response),
    12: DceRpcOp(RpcGetSessionInformation_Request, RpcGetSessionInformation_Response),
    # 13: Opnum13NotUsedOnWire,
    # 14: Opnum14NotUsedOnWire,
    15: DceRpcOp(RpcGetLoggedOnCount_Request, RpcGetLoggedOnCount_Response),
    16: DceRpcOp(RpcGetSessionType_Request, RpcGetSessionType_Response),
    17: DceRpcOp(
        RpcGetSessionInformationEx_Request, RpcGetSessionInformationEx_Response
    ),
    # 18: Opnum18NotUsedOnWire,
    # 19: Opnum19NotUsedOnWire
}
register_dcerpc_interface(
    name="TermSrvSession",
    uuid=uuid.UUID("484809d6-4239-471b-b5bc-61df8c23ac48"),
    version="1.0",
    opnums=TERMSRVSESSION_OPNUMS,
)


class RpcWaitForSessionState_Request(NDRPacket):
    fields_desc = [
        NDRSignedIntField("SessionId", 0),
        NDRSignedIntField("State", 0),
        NDRIntField("Timeout", 0),
    ]


class RpcWaitForSessionState_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcRegisterAsyncNotification_Request(NDRPacket):
    fields_desc = [NDRSignedIntField("SessionId", 0), NDRIntField("Mask", 0)]


class RpcRegisterAsyncNotification_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phNotify", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


class SESSION_CHANGE(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [NDRSignedIntField("SessionId", 0), NDRIntField("NotificationId", 0)]


class RpcWaitAsyncNotification_Request(NDRPacket):
    fields_desc = [NDRPacketField("hNotify", NDRContextHandle(), NDRContextHandle)]


class RpcWaitAsyncNotification_Response(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "SessionChange",
            [],
            SESSION_CHANGE,
            size_is=lambda pkt: pkt.pEntries,
            ptr_pack=True,
        ),
        NDRIntField("pEntries", None, size_of="SessionChange"),
        NDRIntField("status", 0),
    ]


class RpcUnRegisterAsyncNotification_Request(NDRPacket):
    fields_desc = [NDRPacketField("phNotify", NDRContextHandle(), NDRContextHandle)]


class RpcUnRegisterAsyncNotification_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phNotify", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


TERMSRVNOTIFICATION_OPNUMS = {
    0: DceRpcOp(RpcWaitForSessionState_Request, RpcWaitForSessionState_Response),
    1: DceRpcOp(
        RpcRegisterAsyncNotification_Request, RpcRegisterAsyncNotification_Response
    ),
    2: DceRpcOp(RpcWaitAsyncNotification_Request, RpcWaitAsyncNotification_Response),
    3: DceRpcOp(
        RpcUnRegisterAsyncNotification_Request, RpcUnRegisterAsyncNotification_Response
    ),
}
register_dcerpc_interface(
    name="TermSrvNotification",
    uuid=uuid.UUID("11899a43-2b68-4a76-92e3-a3d6ad8c26ce"),
    version="1.0",
    opnums=TERMSRVNOTIFICATION_OPNUMS,
)


class RpcOpenEnum_Request(NDRPacket):
    fields_desc = []


class RpcOpenEnum_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phEnum", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


class RpcCloseEnum_Request(NDRPacket):
    fields_desc = [NDRPacketField("phEnum", NDRContextHandle(), NDRContextHandle)]


class RpcCloseEnum_Response(NDRPacket):
    fields_desc = [
        NDRPacketField("phEnum", NDRContextHandle(), NDRContextHandle),
        NDRIntField("status", 0),
    ]


class RpcFilterByState_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle),
        NDRSignedIntField("State", 0),
        NDRSignedIntField("bInvert", 0),
    ]


class RpcFilterByState_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcFilterByCallersName_Request(NDRPacket):
    fields_desc = [NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle)]


class RpcFilterByCallersName_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class RpcEnumAddFilter_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle),
        NDRPacketField("hSubEnum", NDRContextHandle(), NDRContextHandle),
    ]


class RpcEnumAddFilter_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class SESSIONENUM_LEVEL1(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRSignedIntField("SessionId", 0),
        NDRSignedIntField("State", 0),
        StrFixedLenFieldUtf16("Name", "", length=33 * 2),
    ]


class GUID(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRIntField("Data1", 0),
        NDRShortField("Data2", 0),
        NDRShortField("Data3", 0),
        StrFixedLenField("Data4", "", length=8),
    ]


class SESSIONENUM_LEVEL2(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRSignedIntField("SessionId", 0),
        NDRSignedIntField("State", 0),
        StrFixedLenFieldUtf16("Name", "", length=33 * 2),
        NDRIntField("Source", 0),
        NDRSignedIntField("bFullDesktop", 0),
        NDRPacketField("SessionType", GUID(), GUID),
    ]


class PSESSIONENUM(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRIntField("Level", 0),
        NDRUnionField(
            [
                (
                    NDRPacketField("Data", SESSIONENUM_LEVEL1(), SESSIONENUM_LEVEL1),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 1),
                        (lambda _, val: val.tag == 1),
                    ),
                ),
                (
                    NDRPacketField("Data", SESSIONENUM_LEVEL2(), SESSIONENUM_LEVEL2),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 2),
                        (lambda _, val: val.tag == 2),
                    ),
                ),
            ],
            StrFixedLenField("Data", "", length=0),
            align=(4, 4),
            switch_fmt=("L", "L"),
        ),
    ]


class RpcGetEnumResult_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle),
        NDRIntField("Level", 0),
    ]


class RpcGetEnumResult_Response(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "ppSessionEnumResult",
            [],
            PSESSIONENUM,
            size_is=lambda pkt: pkt.pEntries,
            ptr_pack=True,
        ),
        NDRIntField("pEntries", None, size_of="ppSessionEnumResult"),
        NDRIntField("status", 0),
    ]


class RpcFilterBySessionType_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle),
        NDRPacketField("pSessionType", GUID(), GUID),
    ]


class RpcFilterBySessionType_Response(NDRPacket):
    fields_desc = [NDRIntField("status", 0)]


class SESSION_FILTER(IntEnum):
    SF_SERVICES_SESSION_POPUP = 1


class RpcGetSessionIds_Request(NDRPacket):
    fields_desc = [
        NDRInt3264EnumField("Filter", 0, SESSION_FILTER),
        NDRIntField("MaxEntries", 0),
    ]


class RpcGetSessionIds_Response(NDRPacket):
    fields_desc = [
        NDRConfFieldListField(
            "pSessionIds",
            [],
            NDRSignedIntField("", 0),
            size_is=lambda pkt: pkt.pcSessionIds,
            ptr_pack=True,
        ),
        NDRIntField("pcSessionIds", None, size_of="pSessionIds"),
        NDRIntField("status", 0),
    ]


class SESSIONENUM_LEVEL3(NDRPacket):
    ALIGNMENT = (4, 8)
    fields_desc = [
        NDRSignedIntField("SessionId", 0),
        NDRSignedIntField("State", 0),
        StrFixedLenFieldUtf16("Name", "", length=33 * 2),
        NDRIntField("Source", 0),
        NDRSignedIntField("bFullDesktop", 0),
        NDRPacketField("SessionType", GUID(), GUID),
        NDRIntField("ProtoDataSize", None, size_of="pProtocolData"),
        NDRFullPointerField(
            NDRConfStrLenField(
                "pProtocolData", "", size_is=lambda pkt: pkt.ProtoDataSize
            ),
            deferred=True,
        ),
    ]


class PSESSIONENUM_EX(NDRPacket):
    ALIGNMENT = (4, 8)
    fields_desc = [
        NDRIntField("Level", 0),
        NDRUnionField(
            [
                (
                    NDRPacketField("Data", SESSIONENUM_LEVEL1(), SESSIONENUM_LEVEL1),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 1),
                        (lambda _, val: val.tag == 1),
                    ),
                ),
                (
                    NDRPacketField("Data", SESSIONENUM_LEVEL2(), SESSIONENUM_LEVEL2),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 2),
                        (lambda _, val: val.tag == 2),
                    ),
                ),
                (
                    NDRPacketField("Data", SESSIONENUM_LEVEL3(), SESSIONENUM_LEVEL3),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 3),
                        (lambda _, val: val.tag == 3),
                    ),
                ),
            ],
            StrFixedLenField("Data", "", length=0),
            align=(4, 8),
            switch_fmt=("L", "L"),
        ),
    ]


class RpcGetEnumResultEx_Request(NDRPacket):
    fields_desc = [
        NDRPacketField("hEnum", NDRContextHandle(), NDRContextHandle),
        NDRIntField("Level", 0),
    ]


class RpcGetEnumResultEx_Response(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "ppSessionEnumResult",
            [],
            PSESSIONENUM_EX,
            size_is=lambda pkt: pkt.pEntries,
            ptr_pack=True,
        ),
        NDRIntField("pEntries", None, size_of="ppSessionEnumResult"),
        NDRIntField("status", 0),
    ]


class EXECENVDATA_LEVEL1(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRSignedIntField("ExecEnvId", 0),
        NDRSignedIntField("State", 0),
        StrFixedLenFieldUtf16("SessionName", "", length=33 * 2),
    ]


class EXECENVDATA_LEVEL2(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRSignedIntField("ExecEnvId", 0),
        NDRSignedIntField("State", 0),
        StrFixedLenFieldUtf16("SessionName", "", length=33 * 2),
        NDRSignedIntField("AbsSessionId", 0),
        StrFixedLenFieldUtf16("HostName", "", length=33 * 2),
        StrFixedLenFieldUtf16("UserName", "", length=33 * 2),
        StrFixedLenFieldUtf16("DomainName", "", length=33 * 2),
        StrFixedLenFieldUtf16("FarmName", "", length=33 * 2),
    ]


class PEXECENVDATA(NDRPacket):
    ALIGNMENT = (4, 4)
    fields_desc = [
        NDRIntField("Level", 0),
        NDRUnionField(
            [
                (
                    NDRPacketField("Data", EXECENVDATA_LEVEL1(), EXECENVDATA_LEVEL1),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 1),
                        (lambda _, val: val.tag == 1),
                    ),
                ),
                (
                    NDRPacketField("Data", EXECENVDATA_LEVEL2(), EXECENVDATA_LEVEL2),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 2),
                        (lambda _, val: val.tag == 2),
                    ),
                ),
            ],
            StrFixedLenField("Data", "", length=0),
            align=(4, 4),
            switch_fmt=("L", "L"),
        ),
    ]


class RpcGetAllSessions_Request(NDRPacket):
    fields_desc = [NDRIntField("pLevel", 0)]


class RpcGetAllSessions_Response(NDRPacket):
    fields_desc = [
        NDRIntField("pLevel", 0),
        NDRConfPacketListField(
            "ppSessionData",
            [],
            PEXECENVDATA,
            size_is=lambda pkt: pkt.pcEntries,
            ptr_pack=True,
        ),
        NDRIntField("pcEntries", None, size_of="ppSessionData"),
        NDRIntField("status", 0),
    ]


class EXECENVDATAEX_LEVEL1(NDRPacket):
    ALIGNMENT = (4, 8)
    fields_desc = [
        NDRSignedIntField("ExecEnvId", 0),
        NDRSignedIntField("State", 0),
        NDRSignedIntField("AbsSessionId", 0),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszSessionName", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszHostName", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszUserName", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszDomainName", ""), deferred=True
        ),
        NDRFullPointerField(
            NDRConfVarStrNullFieldUtf16("pszFarmName", ""), deferred=True
        ),
    ]


class PEXECENVDATAEX(NDRPacket):
    ALIGNMENT = (4, 8)
    fields_desc = [
        NDRIntField("Level", 0),
        NDRUnionField(
            [
                (
                    NDRPacketField(
                        "Data", EXECENVDATAEX_LEVEL1(), EXECENVDATAEX_LEVEL1
                    ),
                    (
                        (lambda pkt: getattr(pkt, "Level", None) == 1),
                        (lambda _, val: val.tag == 1),
                    ),
                )
            ],
            StrFixedLenField("Data", "", length=0),
            align=(4, 8),
            switch_fmt=("L", "L"),
        ),
    ]


class RpcGetAllSessionsEx_Request(NDRPacket):
    fields_desc = [NDRIntField("Level", 0)]


class RpcGetAllSessionsEx_Response(NDRPacket):
    fields_desc = [
        NDRConfPacketListField(
            "ppSessionData",
            [],
            PEXECENVDATAEX,
            size_is=lambda pkt: pkt.pcEntries,
            ptr_pack=True,
        ),
        NDRIntField("pcEntries", None, size_of="ppSessionData"),
        NDRIntField("status", 0),
    ]


TERMSRVENUMERATION_OPNUMS = {
    0: DceRpcOp(RpcOpenEnum_Request, RpcOpenEnum_Response),
    1: DceRpcOp(RpcCloseEnum_Request, RpcCloseEnum_Response),
    2: DceRpcOp(RpcFilterByState_Request, RpcFilterByState_Response),
    3: DceRpcOp(RpcFilterByCallersName_Request, RpcFilterByCallersName_Response),
    4: DceRpcOp(RpcEnumAddFilter_Request, RpcEnumAddFilter_Response),
    5: DceRpcOp(RpcGetEnumResult_Request, RpcGetEnumResult_Response),
    6: DceRpcOp(RpcFilterBySessionType_Request, RpcFilterBySessionType_Response),
    # 7: Opnum7NotUsedOnWire,
    8: DceRpcOp(RpcGetSessionIds_Request, RpcGetSessionIds_Response),
    9: DceRpcOp(RpcGetEnumResultEx_Request, RpcGetEnumResultEx_Response),
    10: DceRpcOp(RpcGetAllSessions_Request, RpcGetAllSessions_Response),
    11: DceRpcOp(RpcGetAllSessionsEx_Request, RpcGetAllSessionsEx_Response),
}
register_dcerpc_interface(
    name="TermSrvEnumeration",
    uuid=uuid.UUID("88143fd0-c28d-4b2b-8fef-8d882f6a9390"),
    version="1.0",
    opnums=TERMSRVENUMERATION_OPNUMS,
)
