Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.3 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <ntseapi.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. VOID
  9. DumpSecurityDescriptor(
  10. IN PSECURITY_DESCRIPTOR SecurityDescriptor
  11. )
  12. {
  13. PISECURITY_DESCRIPTOR sd = (PISECURITY_DESCRIPTOR)SecurityDescriptor;
  14. ULONG sdLength = RtlLengthSecurityDescriptor(sd);
  15. PACL dacl;
  16. PACCESS_ALLOWED_ACE ace;
  17. ULONG i, j;
  18. PUCHAR p;
  19. PISID sid;
  20. BOOLEAN selfRelative;
  21. selfRelative = (BOOLEAN)((sd->Control & SE_SELF_RELATIVE) != 0);
  22. printf( " SD:\n" );
  23. printf( " Revision = %x, Control = %x\n", sd->Revision, sd->Control );
  24. printf( " Owner = %x, Group = %x\n", sd->Owner, sd->Group );
  25. printf( " Sacl = %x, Dacl = %x\n", sd->Sacl, sd->Dacl );
  26. if ( (sd->Control & SE_DACL_PRESENT) != 0 ) {
  27. dacl = sd->Dacl;
  28. if ( selfRelative ) {
  29. dacl = (PACL)((PUCHAR)sd + (ULONG)dacl);
  30. }
  31. printf( " DACL:\n" );
  32. printf( " AclRevision = %x, AclSize = %x, AceCount = %x\n",
  33. dacl->AclRevision, dacl->AclSize, dacl->AceCount );
  34. ace = (PACCESS_ALLOWED_ACE)(dacl + 1);
  35. for ( i = 0; i < dacl->AceCount; i++ ) {
  36. printf( " ACE %d:\n", i );
  37. printf( " AceType = %x, AceFlags = %x, AceSize = %x\n",
  38. ace->Header.AceType, ace->Header.AceFlags, ace->Header.AceSize );
  39. if ( ace->Header.AceType < ACCESS_MAX_MS_V2_ACE_TYPE ) {
  40. printf(" Mask = %08x, Sid = ", ace->Mask );
  41. for ( j = FIELD_OFFSET(ACCESS_ALLOWED_ACE,SidStart), p = (PUCHAR)&ace->SidStart;
  42. j < ace->Header.AceSize;
  43. j++, p++ ) {
  44. printf( "%02x ", *p );
  45. }
  46. printf( "\n" );
  47. }
  48. ace = (PACCESS_ALLOWED_ACE)((PUCHAR)ace + ace->Header.AceSize );
  49. }
  50. }
  51. if ( sd->Owner != 0 ) {
  52. sid = sd->Owner;
  53. if ( selfRelative ) {
  54. sid = (PISID)((PUCHAR)sd + (ULONG)sid);
  55. }
  56. printf( " Owner SID:\n" );
  57. printf( " Revision = %x, SubAuthorityCount = %x\n",
  58. sid->Revision, sid->SubAuthorityCount );
  59. printf( " IdentifierAuthority = " );
  60. for ( j = 0; j < 6; j++ ) {
  61. printf( "%02x ", sid->IdentifierAuthority.Value[j] );
  62. }
  63. printf( "\n" );
  64. for ( i = 0; i < sid->SubAuthorityCount; i++ ) {
  65. printf(" SubAuthority %d = ", i );
  66. for ( j = 0, p = (PUCHAR)&sid->SubAuthority[i]; j < 4; j++, p++ ) {
  67. printf( "%02x ", *p );
  68. }
  69. printf( "\n" );
  70. }
  71. }
  72. }
  73. int _cdecl
  74. main(int argc, char * argv[])
  75. {
  76. NTSTATUS status;
  77. OBJECT_ATTRIBUTES objectAttributes;
  78. WCHAR unicodeName[MAX_PATH];
  79. UCHAR SecurityDescriptorBuffer[512];
  80. UNICODE_STRING nameString;
  81. IO_STATUS_BLOCK ioStatusBlock;
  82. ULONG lengthNeeded;
  83. HANDLE fileHandle;
  84. if (argc < 2) {
  85. printf("usage: %s file\n", argv[0]);
  86. return -1;
  87. }
  88. mbstowcs(unicodeName, argv[1], strlen(argv[1]) + 1);
  89. RtlDosPathNameToNtPathName_U(
  90. unicodeName,
  91. &nameString,
  92. NULL,
  93. NULL);
  94. InitializeObjectAttributes(
  95. &objectAttributes,
  96. &nameString,
  97. OBJ_CASE_INSENSITIVE,
  98. NULL,
  99. NULL);
  100. status = NtOpenFile(
  101. &fileHandle,
  102. READ_CONTROL,
  103. &objectAttributes,
  104. &ioStatusBlock,
  105. FILE_SHARE_READ | FILE_SHARE_WRITE,
  106. 0);
  107. if (!NT_SUCCESS(status) || !NT_SUCCESS(ioStatusBlock.Status)) {
  108. printf("%s: NtOpenFile on %wZ failed %lx %lx\n", argv[0], &nameString, status, ioStatusBlock.Status);
  109. return -1;
  110. }
  111. //
  112. // Now read the DACL from the server file.
  113. //
  114. status = NtQuerySecurityObject(
  115. fileHandle,
  116. DACL_SECURITY_INFORMATION,
  117. (PSECURITY_DESCRIPTOR)SecurityDescriptorBuffer,
  118. sizeof(SecurityDescriptorBuffer),
  119. &lengthNeeded);
  120. if (!NT_SUCCESS(status)) {
  121. printf("%s: NtQuerySecurityObject on %wZ failed %lx %lx\n", argv[0], &nameString, status, lengthNeeded);
  122. return -1;
  123. }
  124. DumpSecurityDescriptor((PSECURITY_DESCRIPTOR)SecurityDescriptorBuffer);
  125. return 0;
  126. }