home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / sms / netmon / remapi / remapi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-15  |  19.1 KB  |  534 lines

  1.  
  2. //=============================================================================
  3. //  MODULE: RemAPI.c
  4. //
  5. //  Description:
  6. //
  7. //  Bloodhound Parser DLL for SMB Remote APIs
  8. //
  9. //  Modification History
  10. //
  11. //  Steve Hiskey        07/07/94        Created
  12. //=============================================================================
  13.  
  14. #include "RemAPI.h"
  15.  
  16.  
  17. //=============================================================================
  18. //  Forward references.
  19. //=============================================================================
  20.  
  21. VOID WINAPIV RemAPIFormatSummary(LPPROPERTYINST lpPropertyInst);
  22.  
  23.  
  24.  
  25. //=============================================================================
  26. //  Labeled RemAPI command set.
  27. //=============================================================================
  28.        
  29. LABELED_WORD Commands[] = 
  30. {
  31.     {     0,    "NetShareEnum"},
  32.     {     1,    "NetShareGetInfo"},
  33.     {     2,    "NetShareSetInfo"},
  34.     {     3,    "NetShareAdd"},
  35.     {     4,    "NetShareDel"},
  36.     {     5,    "NetShareCheck"},
  37.     {     6,    "NetSessionEnum"},
  38.     {     7,    "NetSessionGetInfo"},
  39.     {     8,    "NetSessionDel"},
  40.     {     9,    "NetConnectionEnum"},
  41.     {    10,    "NetFileEnum"},
  42.     {    11,    "NetFileGetInfo"},
  43.     {    12,    "NetFileClose"},
  44.     {    13,    "NetServerGetInfo"},
  45.     {    14,    "NetServerSetInfo"},
  46.     {    15,    "NetServerDiskEnum"},
  47.     {    16,    "NetServerAdminCommand"},
  48.     {    17,    "NetAuditOpen"},
  49.     {    18,    "NetAuditClear"},
  50.     {    19,    "NetErrorLogOpen"},
  51.     {    20,    "NetErrorLogClear"},
  52.     {    21,    "NetCharDevEnum"},
  53.     {    22,    "NetCharDevGetInfo"},
  54.     {    23,    "NetCharDevControl"},
  55.     {    24,    "NetCharDevQEnum"},
  56.     {    25,    "NetCharDevQGetInfo"},
  57.     {    26,    "NetCharDevQSetInfo"},
  58.     {    27,    "NetCharDevQPurge"},
  59.     {    28,    "NetCharDevQPurgeSelf"},
  60.     {    29,    "NetMessageNameEnum"},
  61.     {    30,    "NetMessageNameGetInfo"},
  62.     {    31,    "NetMessageNameAdd"},
  63.     {    32,    "NetMessageNameDel"},
  64.     {    33,    "NetMessageNameFwd"},
  65.     {    34,    "NetMessageNameUnFwd"},
  66.     {    35,    "NetMessageBufferSend"},
  67.     {    36,    "NetMessageFileSend"},
  68.     {    37,    "NetMessageLogFileSet"},
  69.     {    38,    "NetMessageLogFileGet"},
  70.     {    39,    "NetServiceEnum"},
  71.     {    40,    "NetServiceInstall"},
  72.     {    41,    "NetServiceControl"},
  73.     {    42,    "NetAccessEnum"},
  74.     {    43,    "NetAccessGetInfo"},
  75.     {    44,    "NetAccessSetInfo"},
  76.     {    45,    "NetAccessAdd"},
  77.     {    46,    "NetAccessDel"},
  78.     {    47,    "NetGroupEnum"},
  79.     {    48,    "NetGroupAdd"},
  80.     {    49,    "NetGroupDel"},
  81.     {    50,    "NetGroupAddUser"},
  82.     {    51,    "NetGroupDelUser"},
  83.     {    52,    "NetGroupGetUsers"},
  84.     {    53,    "NetUserEnum"},
  85.     {    54,    "NetUserAdd"},
  86.     {    55,    "NetUserDel"},
  87.     {    56,    "NetUserGetInfo"},
  88.     {    57,    "NetUserSetInfo"},
  89.     {    58,    "NetUserPasswordSet"},
  90.     {    59,    "NetUserGetGroups"},
  91.     {    60,    "DeadTableEntry"},
  92.     {    61,    "DeadTableEntry"},
  93.     {    62,    "NetWkstaSetUID"},
  94.     {    63,    "NetWkstaGetInfo"},
  95.     {    64,    "NetWkstaSetInfo"},
  96.     {    65,    "NetUseEnum"},
  97.     {    66,    "NetUseAdd"},
  98.     {    67,    "NetUseDel"},
  99.     {    68,    "NetUseGetInfo"},
  100.     {    69,    "NetPrintQEnum"},
  101.     {    70,    "NetPrintQGetInfo"},
  102.     {    71,    "NetPrintQSetInfo"},
  103.     {    72,    "NetPrintQAdd"},
  104.     {    73,    "NetPrintQDel"},
  105.     {    74,    "NetPrintQPause"},
  106.     {    75,    "NetPrintQContinue"},
  107.     {    76,    "NetPrintJobEnum"},
  108.     {    77,    "NetPrintJobGetInfo"},
  109.     {    78,    "NetPrintJobSetInfo"},
  110.     {    79,    "DeadTableEntry"},
  111.     {    80,    "DeadTableEntry"},
  112.     {    81,    "NetPrintJobDel"},
  113.     {    82,    "NetPrintJobPause"},
  114.     {    83,    "NetPrintJobContinue"},
  115.     {    84,    "NetPrintDestEnum"},
  116.     {    85,    "NetPrintDestGetInfo"},
  117.     {    86,    "NetPrintDestControl"},
  118.     {    87,    "NetProfileSave"},
  119.     {    88,    "NetProfileLoad"},
  120.     {    89,    "NetStatisticsGet"},
  121.     {    90,    "NetStatisticsClear"},
  122.     {    91,    "NetRemoteTOD"},
  123.     {    92,    "NetBiosEnum"},
  124.     {    93,    "NetBiosGetInfo"},
  125.     {    94,    "NetServerEnum"},
  126.     {    95,    "I_NetServerEnum"},
  127.     {    96,    "NetServiceGetInfo"},
  128.     {    97,    "DeadTableEntry"},
  129.     {    98,    "DeadTableEntry"},
  130.     {    99,    "DeadTableEntry"},
  131.     {   100,    "DeadTableEntry"},
  132.     {   101,    "DeadTableEntry"},
  133.     {   102,    "DeadTableEntry"},
  134.     {   103,    "NetPrintQPurge"},
  135.     {   104,    "NetServerEnum2"},
  136.     {   105,    "NetAccessGetUserPerms"},
  137.     {   106,    "NetGroupGetInfo"},
  138.     {   107,    "NetGroupSetInfo"},
  139.     {   108,    "NetGroupSetUsers"},
  140.     {   109,    "NetUserSetGroups"},
  141.     {   110,    "NetUserModalsGet"},
  142.     {   111,    "NetUserModalsSet"},
  143.     {   112,    "NetFileEnum2"},
  144.     {   113,    "NetUserAdd2"},
  145.     {   114,    "NetUserSetInfo2"},
  146.     {   115,    "NetUserPasswordSet2"},
  147.     {   116,    "I_NetServerEnum2"},
  148.     {   117,    "NetConfigGet2"},
  149.     {   118,    "NetConfigGetAll2"},
  150.     {   119,    "NetGetDCName"},
  151.     {   120,    "NetHandleGetInfo"},
  152.     {   121,    "NetHandleSetInfo"},
  153.     {   122,    "NetStatisticsGet2"},
  154.     {   123,    "NetBuildGetInfo"},
  155.     {   124,    "NetFileGetInfo2"},
  156.     {   125,    "NetFileClose2"},
  157.     {   126,    "NetServerReqChallenge"},
  158.     {   127,    "NetServerAuthenticate"},
  159.     {   128,    "NetServerPasswordSet"},
  160.     {   129,    "NetAccountDeltas"},
  161.     {   130,    "NetAccountSync"},
  162.     {   131,    "NetUserEnum2"},
  163.     {   132,    "NetWkstaUserLogon"},
  164.     {   133,    "NetWkstaUserLogoff"},
  165.     {   134,    "NetLogonEnum"},
  166.     {   135,    "NetErrorLogRead"},
  167.     {   136,    "I_NetPathType"},
  168.     {   137,    "I_NetPathCanonicalize"},
  169.     {   138,    "I_NetPathCompare"},
  170.     {   139,    "I_NetNameValidate"},
  171.     {   140,    "I_NetNameCanonicalize"},
  172.     {   141,    "I_NetNameCompare"},
  173.     {   142,    "NetAuditRead"},
  174.     {   143,    "NetPrintDestAdd"},
  175.     {   144,    "NetPrintDestSetInfo"},
  176.     {   145,    "NetPrintDestDel"},
  177.     {   146,    "NetUserValidate2"},
  178.     {   147,    "NetPrintJobSetInfo"},
  179.     {   148,    "TI_NetServerDiskEnum"},
  180.     {   149,    "TI_NetServerDiskGetInfo"},
  181.     {   150,    "TI_FTVerifyMirror"},
  182.     {   151,    "TI_FTAbortVerify"},
  183.     {   152,    "TI_FTGetInfo"},
  184.     {   153,    "TI_FTSetInfo"},
  185.     {   154,    "TI_FTLockDisk"},
  186.     {   155,    "TI_FTFixError"},
  187.     {   156,    "TI_FTAbortFix"},
  188.     {   157,    "TI_FTDiagnoseError"},
  189.     {   158,    "TI_FTGetDriveStats"},
  190.     {   159,    "DeadTableEntry"},
  191.     {   160,    "TI_FTErrorGetInfo"},
  192.     {   161,    "DeadTableEntry"},
  193.     {   162,    "DeadTableEntry"},
  194.     {   163,    "NetAccessCheck"},
  195.     {   164,    "NetAlertRaise"},
  196.     {   165,    "NetAlertStart"},
  197.     {   166,    "NetAlertStop"},
  198.     {   167,    "NetAuditWrite"},
  199.     {   168,    "NetIRemoteAPI"},
  200.     {   169,    "NetServiceStatus"},
  201.     {   170,    "I_NetServerRegister"},
  202.     {   171,    "I_NetServerDeregister"},
  203.     {   172,    "I_NetSessionEntryMake"},
  204.     {   173,    "I_NetSessionEntryClear"},
  205.     {   174,    "I_NetSessionEntryGetInfo"},
  206.     {   175,    "I_NetSessionEntrySetInfo"},
  207.     {   176,    "I_NetConnectionEntryMake"},
  208.     {   177,    "I_NetConnectionEntryClear"},
  209.     {   178,    "I_NetConnectionEntrySetInfo"},
  210.     {   179,    "I_NetConnectionEntryGetInfo"},
  211.     {   180,    "I_NetFileEntryMake"},
  212.     {   181,    "I_NetFileEntryClear"},
  213.     {   182,    "I_NetFileEntrySetInfo"},
  214.     {   183,    "I_NetFileEntryGetInfo"},
  215.     {   184,    "AltSrvMessageBufferSend"},
  216.     {   185,    "AltSrvMessageFileSend"},
  217.     {   186,    "I_NetRplWkstaEnum"},
  218.     {   187,    "I_NetRplWkstaGetInfo"},
  219.     {   188,    "I_NetRplWkstaSetInfo"},
  220.     {   189,    "I_NetRplWkstaAdd"},
  221.     {   190,    "I_NetRplWkstaDel"},
  222.     {   191,    "I_NetRplProfileEnum"},
  223.     {   192,    "I_NetRplProfileGetInfo"},
  224.     {   193,    "I_NetRplProfileSetInfo"},
  225.     {   194,    "I_NetRplProfileAdd"},
  226.     {   195,    "I_NetRplProfileDel"},
  227.     {   196,    "I_NetRplProfileClone"},
  228.     {   197,    "I_NetRplBaseProfileEnum"},
  229.     {   198,    "DeadTableEntry"},
  230.     {   199,    "DeadTableEntry"},
  231.     {   200,    "DeadTableEntry"},
  232.     {   201,    "NetServerSetInfo"},
  233.     {   202,    "DeadTableEntry"},
  234.     {   203,    "DeadTableEntry"},
  235.     {   204,    "DeadTableEntry"},
  236.     {   205,    "NetPrintDriverEnum"},
  237.     {   206,    "NetPrintQProcessorEnum"},
  238.     {   207,    "NetPrintPortEnum"},
  239.     {   208,    "NetWriteUpdateLog"},
  240.     {   209,    "NetAccountUpdate"},
  241.     {   210,    "NetAccountConfirmUpdate"},
  242.     {   211,    "NetConfigSet"},
  243.     {   212,    "NetAccountsReplicate"},
  244. };
  245.  
  246. SET CommandSET = { (sizeof Commands / sizeof(LABELED_BYTE)), Commands };
  247.  
  248.  
  249. //=============================================================================
  250. //  RemAPI database.
  251. //=============================================================================
  252.  
  253. #define REMAPI_SUMMARY      0
  254. #define REMAPI_COMMAND      1
  255.  
  256. PROPERTYINFO RemAPIDatabase[] =
  257. {
  258.     {   //  REMAPI_SUMMARY
  259.         0,0, 
  260.         "Summary",  
  261.         "Remote API packet", 
  262.         PROP_TYPE_SUMMARY, 
  263.         PROP_QUAL_NONE, 
  264.         0, 
  265.         FORMAT_BUFFER_SIZE, 
  266.         RemAPIFormatSummary},
  267.  
  268.     {   // REMAPI_COMMAND
  269.         0,0, 
  270.         "Command",     
  271.         "Remote API Command field.", 
  272.         PROP_TYPE_WORD,    
  273.         PROP_QUAL_LABELED_SET, 
  274.         &CommandSET, 
  275.         FORMAT_BUFFER_SIZE, 
  276.         FormatPropertyInstance},
  277. };
  278.  
  279. DWORD nRemAPIProperties = ((sizeof RemAPIDatabase) / PROPERTYINFO_SIZE);
  280.  
  281.  
  282.  
  283. //=============================================================================
  284. //  Protocol entry points.
  285. //=============================================================================
  286.  
  287. VOID   WINAPI RemAPIRegister(HPROTOCOL);
  288. VOID   WINAPI RemAPIDeregister(HPROTOCOL);
  289. LPBYTE WINAPI RemAPIRecognizeFrame(HFRAME, LPVOID, LPVOID, DWORD, DWORD, HPROTOCOL, DWORD, LPDWORD, LPHPROTOCOL, LPDWORD);
  290. LPBYTE WINAPI RemAPIAttachProperties(HFRAME, LPVOID, LPVOID, DWORD, DWORD, HPROTOCOL, DWORD, DWORD);
  291. DWORD  WINAPI RemAPIFormatProperties(HFRAME, LPVOID, LPVOID, DWORD, LPPROPERTYINST);
  292.  
  293. ENTRYPOINTS RemAPIEntryPoints =
  294. {
  295.     RemAPIRegister,
  296.     RemAPIDeregister,
  297.     RemAPIRecognizeFrame,
  298.     RemAPIAttachProperties,
  299.     RemAPIFormatProperties
  300. };
  301.  
  302. HPROTOCOL hRemAPI = NULL;
  303.  
  304.  
  305. DWORD Attached = 0;
  306.  
  307. //=============================================================================
  308. //  FUNCTION: DLLEntry()
  309. //
  310. //  Modification History
  311. //
  312. //  Steve Hiskey        07/07/94        Created
  313. //=============================================================================
  314.  
  315. BOOL WINAPI DLLEntry(HANDLE hInstance, ULONG Command, LPVOID Reserved)
  316. {
  317.     //=========================================================================
  318.     //  If we are loading!
  319.     //=========================================================================
  320.  
  321.     if ( Command == DLL_PROCESS_ATTACH )
  322.     {
  323.         if ( Attached++ == 0 )
  324.         {
  325.             hRemAPI = CreateProtocol("RemAPI", &RemAPIEntryPoints, ENTRYPOINTS_SIZE);
  326.         }
  327.     }
  328.  
  329.     //=========================================================================
  330.     //  If we are unloading!
  331.     //=========================================================================
  332.  
  333.     if ( Command == DLL_PROCESS_DETACH )
  334.     {
  335.         if ( --Attached == 0 )
  336.         {
  337.             DestroyProtocol(hRemAPI);
  338.         }
  339.     }
  340.  
  341.     return TRUE;                    //... Bloodhound parsers ALWAYS return TRUE.
  342. }
  343.  
  344.  
  345. //=============================================================================
  346. //  FUNCTION: RemAPIRegister()
  347. //
  348. //  Modification History
  349. //
  350. //  Steve Hiskey        07/07/94        Created
  351. //=============================================================================
  352.  
  353. VOID WINAPI RemAPIRegister(HPROTOCOL hRemAPIProtocol)
  354. {
  355.     register DWORD i;
  356.  
  357.     //=========================================================================
  358.     //  Create the property database.
  359.     //=========================================================================
  360.  
  361.     CreatePropertyDatabase(hRemAPIProtocol, nRemAPIProperties);
  362.  
  363.     for(i = 0; i < nRemAPIProperties; ++i)
  364.     {
  365.         AddProperty(hRemAPIProtocol, &RemAPIDatabase[i]);
  366.     }
  367.  
  368. }
  369.  
  370. //=============================================================================
  371. //  FUNCTION: Deregister()
  372. //
  373. //  Modification History
  374. //
  375. //  Steve Hiskey        07/07/94        Created
  376. //=============================================================================
  377.  
  378. VOID WINAPI RemAPIDeregister(HPROTOCOL hRemAPIProtocol)
  379. {
  380.     DestroyPropertyDatabase(hRemAPIProtocol);
  381. }
  382.  
  383. //=============================================================================
  384. //  FUNCTION: RemAPIRecognizeFrame()
  385. //
  386. //  Modification History
  387. //
  388. //  Steve Hiskey        07/07/94        Created
  389. //=============================================================================
  390.  
  391. LPBYTE WINAPI RemAPIRecognizeFrame(HFRAME          hFrame,                     //... frame handle.
  392.                                 LPBYTE          MacFrame,                   //... Frame pointer.
  393.                                 LPBYTE          RemAPIFrame,                   //... Relative pointer.
  394.                                 DWORD           MacType,                    //... MAC type.
  395.                                 DWORD           BytesLeft,                  //... Bytes left.
  396.                                 HPROTOCOL       hPreviousProtocol,          //... Previous protocol or NULL if none.
  397.                                 DWORD           nPreviousProtocolOffset,    //... Offset of previous protocol.
  398.                                 LPDWORD         ProtocolStatusCode,         //... Pointer to return status code in.
  399.                                 LPHPROTOCOL     hNextProtocol,              //... Next protocol to call (optional).
  400.                                 LPDWORD         InstData)                   //... Next protocol instance data.
  401. {
  402.     *ProtocolStatusCode = PROTOCOL_STATUS_CLAIMED;
  403.     return NULL;
  404. }
  405.  
  406. //=============================================================================
  407. //  FUNCTION: RemAPIAttachProperties()
  408. //
  409. //  Modification History
  410. //
  411. //  Steve Hiskey        07/07/94        Created
  412. //=============================================================================
  413.  
  414. LPBYTE WINAPI RemAPIAttachProperties(HFRAME    hFrame,
  415.                                   LPBYTE    Frame,
  416.                                   LPBYTE    RemAPIFrame,
  417.                                   DWORD     MacType,
  418.                                   DWORD     BytesLeft,
  419.                                   HPROTOCOL hPreviousProtocol,
  420.                                   DWORD     nPreviousProtocolOffset,
  421.                                   DWORD     InstData)
  422. {
  423.     if ( InstData == 0 )    // this is a request... SMB said so...
  424.     {
  425.         AttachPropertyInstance(hFrame,
  426.                            RemAPIDatabase[REMAPI_SUMMARY].hProperty,
  427.                            BytesLeft,
  428.                            RemAPIFrame,
  429.                            0, 0, 0);
  430.  
  431.         AttachPropertyInstance(hFrame,
  432.                            RemAPIDatabase[REMAPI_COMMAND].hProperty,
  433.                            sizeof(WORD),
  434.                            RemAPIFrame,
  435.                            0, 
  436.                            1,   // level
  437.                            0);
  438.  
  439.     }
  440.     else    // this is a response SMB transact... we don't get told WHAT command the
  441.             // request was... so we have to look back to find the previous frame that 
  442.             // contains our request.
  443.             // This parser assumes that the SMB parser is kind in that it will tell
  444.             // us the frame number of the request frame.  Without this, we would have
  445.             // to get the SMB mid/pid/tid/uid from our frame, find the previous SMB
  446.             // that matches our description, and see what our Remote API command was.
  447.     {
  448.         // The InstData is actually the frame number of the frame that holds the request..
  449.         // If the frame number was 0, then the frame number will be (DWORD)-1.
  450.         DWORD   ReqFrameNum = (InstData==(DWORD)-1)?0:InstData;
  451.         WORD    RemAPICommand;
  452.         HFRAME  hFrameReq;
  453.         LPBYTE  lpReqRemAPI;
  454.         LPBYTE  lpReqFrame;
  455.  
  456.         // Go get that frame and see what type of remote API we are...
  457.         hFrameReq = GetFrame ( GetFrameCaptureHandle(hFrame), ReqFrameNum );
  458.  
  459.         if ( hFrameReq == NULL )    // we have a frame number but no frame??
  460.         {   // This should never happen because the SMB parser SAID that we were
  461.             // a remote api AND it gave us a frame number... therefore, hitting
  462.             // this code path implies a bug in either SMB or the kernel.
  463.             #ifdef DEBUG
  464.             DebugBreak ();
  465.             #endif
  466.             return NULL;
  467.         }
  468.         
  469.         lpReqFrame = LockFrame ( hFrameReq );
  470.         lpReqRemAPI = lpReqFrame + GetProtocolStartOffset ( hFrameReq, "REMAPI" );
  471.         RemAPICommand = *(UNALIGNED WORD *)lpReqRemAPI;  // command is the first word.
  472.  
  473.         // NOW we can attach properties for the response
  474.  
  475.  
  476.  
  477.  
  478.         UnlockFrame ( hFrameReq );
  479.     }
  480.     return NULL;
  481. }
  482.  
  483.  
  484. //==============================================================================
  485. //  FUNCTION: RemAPIFormatSummary()
  486. //
  487. //  Modification History
  488. //
  489. //  Steve Hiskey        07/07/94        Created
  490. //==============================================================================
  491.  
  492. VOID WINAPIV RemAPIFormatSummary(LPPROPERTYINST lpPropertyInst)
  493. {
  494.     WORD Command = *(UNALIGNED WORD *)lpPropertyInst->lpData;
  495.     LPSTR CommandStr = LookupWordSetString ( &CommandSET, Command );
  496.  
  497.     DWORD Length = wsprintf(lpPropertyInst->szPropertyText, 
  498.                             "Remote API %s",
  499.                             CommandStr?CommandStr:"Unknown" );
  500. }
  501.  
  502.  
  503. //==============================================================================
  504. //  FUNCTION: RemAPIFormatProperties()
  505. //
  506. //  Modification History
  507. //
  508. //  Steve Hiskey        07/07/94        Created
  509. //==============================================================================
  510.  
  511. DWORD WINAPI RemAPIFormatProperties(HFRAME         hFrame,
  512.                                  LPBYTE         MacFrame,
  513.                                  LPBYTE         FrameData,
  514.                                  DWORD          nPropertyInsts,
  515.                                  LPPROPERTYINST p)
  516. {
  517.     //=========================================================================
  518.     //  Format each property in the property instance table.
  519.     //
  520.     //  The property-specific instance data was used to store the address of a
  521.     //  property-specific formatting function so all we do here is call each
  522.     //  function via the instance data pointer.
  523.     //=========================================================================
  524.  
  525.     while (nPropertyInsts--)
  526.     {
  527.         ((FORMAT) p->lpPropertyInfo->InstanceData)(p);
  528.  
  529.         p++;
  530.     }
  531.  
  532.     return BHERR_SUCCESS;
  533. }
  534.