dumpsd.c
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Visual C++

  1. #ifdef DEBUG
  2. void HexDumpBytes(
  3. TCHAR *pv,
  4. unsigned cb)
  5. {
  6. TCHAR achHex[]=TEXT("0123456789ABCDEF");
  7. TCHAR achOut[80];
  8. unsigned iOut;
  9. iOut = 0;
  10. while (cb)
  11.    {
  12.    if (iOut >= 78)
  13.       {
  14.       PINFO(achOut);
  15.       iOut = 0;
  16.       }
  17.    achOut[iOut++] = achHex[(*pv >> 4) & 0x000f];
  18.    achOut[iOut++] = achHex[*pv++ & 0x000f];
  19.    achOut[iOut]   = TEXT('');
  20.    cb--;
  21.    }
  22. if (iOut)
  23.    {
  24.    PINFO(achOut);
  25.    }
  26. }
  27. void PrintSid(
  28. PSID sid)
  29. {
  30. DWORD cSubAuth;
  31. DWORD i;
  32. PINFO(TEXT("rnSID: "));
  33. if (sid)
  34.    {
  35.    HexDumpBytes((LPBYTE)GetSidIdentifierAuthority(sid), sizeof(SID_IDENTIFIER_AUTHORITY));
  36.    SetLastError(0);
  37.    cSubAuth = *GetSidSubAuthorityCount(sid);
  38.    if (GetLastError())
  39.       {
  40.       PINFO(TEXT("Invalid SIDrn"));
  41.       }
  42.    else
  43.       {
  44.       for (i = 0;i < cSubAuth; i++)
  45.          {
  46.          PINFO(TEXT("-"));
  47.          HexDumpBytes((LPBYTE)GetSidSubAuthority(sid, i), sizeof(DWORD));
  48.          }
  49.       PINFO(TEXT("rn"));
  50.       }
  51.    }
  52. else
  53.    {
  54.    PINFO(TEXT("NULL SIDrn"));
  55.    }
  56. }
  57. //
  58. // Purpose: Print out the entries in an access-control list.
  59. //
  60. void PrintAcl(
  61. PACL pacl)
  62. {
  63. ACL_SIZE_INFORMATION aclsi;
  64. ACCESS_ALLOWED_ACE  *pace;
  65. unsigned             i;
  66. if (pacl)
  67.    {
  68.    if (GetAclInformation(pacl, &aclsi, sizeof(aclsi), AclSizeInformation))
  69.       {
  70.       for (i = 0;i < aclsi.AceCount;i++)
  71.          {
  72.          GetAce(pacl, i, &pace);
  73.          PINFO(TEXT("Type(%x) Flags(%x) Access(%lx)rnSID:"),(int)pace->Header.AceType,
  74.                (int)pace->Header.AceFlags, pace->Mask);
  75.          PrintSid((PSID)&(pace->SidStart));
  76.          }
  77.       }
  78.    }
  79. else
  80.    {
  81.    PINFO(TEXT("NULL PACLrn"));
  82.    }
  83. }
  84. void PrintSD(
  85. PSECURITY_DESCRIPTOR pSD)
  86. {
  87. DWORD dwRev;
  88. WORD  wSDC;
  89. BOOL  fDefault, fAcl;
  90. PACL  pacl;
  91. PSID  sid;
  92. if (NULL == pSD)
  93.    {
  94.    PINFO(TEXT("NULL sdrn"));
  95.    return;
  96.    }
  97. if (!IsValidSecurityDescriptor(pSD))
  98.    {
  99.    PINFO(TEXT("Bad SD %p"), pSD);
  100.    return;
  101.    }
  102. // Drop control info and revision
  103. if (GetSecurityDescriptorControl(pSD, &wSDC, &dwRev))
  104.    {
  105.    PINFO(TEXT("SD - Length: [%ld] Control: [%x] [%lx]rnGroup:"),
  106.          GetSecurityDescriptorLength(pSD), wSDC, dwRev);
  107.    }
  108. else
  109.    {
  110.    PINFO(TEXT("Couldn't get controlrnGroup"));
  111.    }
  112. // Show group and owner
  113. if (GetSecurityDescriptorGroup(pSD, &sid, &fDefault) &&
  114.     sid &&
  115.     IsValidSid(sid))
  116.    {
  117.    PrintSid(sid);
  118.    PINFO(TEXT(" %s default.rnOwner:"), fDefault ? TEXT(" ") : TEXT("Not"));
  119.    }
  120. else
  121.    {
  122.    PINFO(TEXT("Couldn't get grouprn"));
  123.    }
  124. if (GetSecurityDescriptorOwner(pSD, &sid, &fDefault) &&
  125.     sid &&
  126.     IsValidSid(sid))
  127.    {
  128.    PrintSid(sid);
  129.    PINFO(TEXT(" %s default.rn"), fDefault ? TEXT(" ") : TEXT("Not"));
  130.    }
  131. else
  132.    {
  133.    PINFO(TEXT("Couldn't get ownerrn"));
  134.    }
  135. // Print DACL and SACL
  136. if (GetSecurityDescriptorDacl(pSD, &fAcl, &pacl, &fDefault))
  137.    {
  138.    PINFO(TEXT("DACL: %s %srn"), fAcl ? TEXT("Yes") : TEXT("No"),
  139.          fDefault ? TEXT("Default") : TEXT(" "));
  140.    if (fAcl)
  141.       {
  142.       if (pacl && IsValidAcl(pacl))
  143.          {
  144.          PrintAcl(pacl);
  145.          }
  146.       else
  147.          {
  148.          PINFO(TEXT("Invalid Acl %prn"), pacl);
  149.          }
  150.       }
  151.    }
  152. else
  153.    {
  154.    PINFO(TEXT("Couldn't get DACLrn"));
  155.    }
  156. if (GetSecurityDescriptorSacl(pSD, &fAcl, &pacl, &fDefault))
  157.    {
  158.    PINFO(TEXT("SACL: %s %srn"), fAcl ? TEXT("Yes") : TEXT("No"),
  159.          fDefault ? TEXT("Default") : TEXT(" "));
  160.    if (fAcl)
  161.       {
  162.       if (pacl && IsValidAcl(pacl))
  163.          {
  164.          PrintAcl(pacl);
  165.          }
  166.       else
  167.          {
  168.          PINFO(TEXT("Invalid ACL %prn"), pacl);
  169.          }
  170.       }
  171.    }
  172. else
  173.    {
  174.    PINFO(TEXT("Couldn't get SACLrn"));
  175.    }
  176. }
  177. #else
  178. #define PrintSid(x)
  179. #define PrintSD(x)
  180. #endif