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.

332 lines
7.9 KiB

  1. // Include NT headers
  2. #include <nt.h>
  3. #include <ntrtl.h>
  4. #include <nturtl.h>
  5. #include <ntseapi.h>
  6. #include "windows.h"
  7. void CtxDumpSid( PSID, PCHAR, PULONG );
  8. void DumpAcl( PACL, PCHAR, PULONG );
  9. void DumpAce( PACE_HEADER, PCHAR, PULONG );
  10. #if DBG
  11. void
  12. DumpSecurityDescriptor(
  13. PSECURITY_DESCRIPTOR pSD
  14. )
  15. {
  16. PISECURITY_DESCRIPTOR p = (PISECURITY_DESCRIPTOR)pSD;
  17. PSID pSid;
  18. PACL pAcl;
  19. PCHAR pTmp;
  20. ULONG Size;
  21. //
  22. // This is done under an exception handler in case someone passes in
  23. // a totally bogus security descriptor
  24. //
  25. try {
  26. DbgPrint("DUMP_SECURITY_DESCRIPTOR: Revision %d, Sbz1 %d, Control 0x%x\n",
  27. p->Revision, p->Sbz1, p->Control );
  28. if ( p->Control & SE_SELF_RELATIVE ) {
  29. DbgPrint("Self Relative\n");
  30. }
  31. DbgPrint("PSID Owner 0x%x\n",p->Owner);
  32. // If this is self relative, must offset the pointers
  33. if( p->Owner != NULL ) {
  34. if( p->Control & SE_SELF_RELATIVE ) {
  35. pTmp = (PCHAR)pSD;
  36. pTmp += (UINT_PTR)p->Owner;
  37. CtxDumpSid( (PSID)pTmp, (PCHAR)p, &Size );
  38. }
  39. else {
  40. // can reference it directly
  41. CtxDumpSid( p->Owner, (PCHAR)p, &Size );
  42. }
  43. }
  44. DbgPrint("PSID Group 0x%x\n",p->Group);
  45. // If this is self relative, must offset the pointers
  46. if( p->Group != NULL ) {
  47. if( p->Control & SE_SELF_RELATIVE ) {
  48. pTmp = (PCHAR)pSD;
  49. pTmp += (UINT_PTR)p->Group;
  50. CtxDumpSid( (PSID)pTmp, (PCHAR)p, &Size );
  51. }
  52. else {
  53. // can reference it directly
  54. CtxDumpSid( p->Group, (PCHAR)p, &Size );
  55. }
  56. }
  57. DbgPrint("\n");
  58. DbgPrint("PACL Sacl 0x%x\n",p->Sacl);
  59. // If this is self relative, must offset the pointers
  60. if( p->Sacl != NULL ) {
  61. if( p->Control & SE_SELF_RELATIVE ) {
  62. pTmp = (PCHAR)pSD;
  63. pTmp += (UINT_PTR)p->Sacl;
  64. DumpAcl( (PSID)pTmp, (PCHAR)p, &Size );
  65. }
  66. else {
  67. // can reference it directly
  68. DumpAcl( p->Sacl, (PCHAR)p, &Size );
  69. }
  70. }
  71. DbgPrint("\n");
  72. DbgPrint("PACL Dacl 0x%x\n",p->Dacl);
  73. // If this is self relative, must offset the pointers
  74. if( p->Dacl != NULL ) {
  75. if( p->Control & SE_SELF_RELATIVE ) {
  76. pTmp = (PCHAR)pSD;
  77. pTmp += (UINT_PTR)p->Dacl;
  78. DumpAcl( (PSID)pTmp, (PCHAR)p, &Size );
  79. }
  80. else {
  81. // can reference it directly
  82. DumpAcl( p->Dacl, (PCHAR)p, &Size );
  83. }
  84. }
  85. } except( EXCEPTION_EXECUTE_HANDLER) {
  86. DbgPrint("DUMP_SECURITY_DESCRIPTOR: Exception %d accessing descriptor\n",GetExceptionCode());
  87. return;
  88. }
  89. }
  90. #endif
  91. #if DBG
  92. void
  93. CtxDumpSid(
  94. PSID pSid,
  95. PCHAR pBase,
  96. PULONG pSize
  97. )
  98. {
  99. PISID p;
  100. ULONG i;
  101. BOOL OK;
  102. DWORD szUserName;
  103. DWORD szDomain;
  104. SID_NAME_USE UserSidType;
  105. WCHAR UserName[256];
  106. WCHAR Domain[256];
  107. ULONG Size = 0;
  108. p = (PISID)pSid;
  109. DbgPrint("Revision %d, SubAuthorityCount %d\n", p->Revision, p->SubAuthorityCount);
  110. Size += 2; // Revision, SubAuthorityCount
  111. DbgPrint("IdentifierAuthority: %x %x %x %x %x %x\n",
  112. p->IdentifierAuthority.Value[0],
  113. p->IdentifierAuthority.Value[1],
  114. p->IdentifierAuthority.Value[2],
  115. p->IdentifierAuthority.Value[3],
  116. p->IdentifierAuthority.Value[4],
  117. p->IdentifierAuthority.Value[5] );
  118. Size += 6; // IdentifierAuthority
  119. for( i=0; i < p->SubAuthorityCount; i++ ) {
  120. DbgPrint("SubAuthority[%d] 0x%x\n", i, p->SubAuthority[i]);
  121. Size += sizeof(ULONG);
  122. }
  123. if( pSize ) {
  124. *pSize = Size;
  125. }
  126. szUserName = sizeof(UserName);
  127. szDomain = sizeof(Domain);
  128. // Now print its account
  129. OK = LookupAccountSidW(
  130. NULL, // Computer Name
  131. pSid,
  132. UserName,
  133. &szUserName,
  134. Domain,
  135. &szDomain,
  136. &UserSidType
  137. );
  138. if( OK ) {
  139. DbgPrint("Account Name %ws, Domain %ws, Type %d, SidSize %d\n",UserName,Domain,UserSidType,Size);
  140. }
  141. else {
  142. DbgPrint("Error looking up account name %d, SizeSid %d\n",GetLastError(),Size);
  143. }
  144. }
  145. #endif
  146. #if DBG
  147. void
  148. DumpAcl(
  149. PACL pAcl,
  150. PCHAR pBase,
  151. PULONG pSize
  152. )
  153. {
  154. USHORT i;
  155. PCHAR pTmp;
  156. ULONG Size, MySize;
  157. PACL p = pAcl;
  158. PCHAR pCur = (PCHAR)pAcl;
  159. MySize = 0;
  160. DbgPrint("AclRevision %d, Sbz1 %d, AclSize %d, AceCount %d, Sbz2 %d\n",
  161. p->AclRevision, p->Sbz1, p->AclSize, p->AceCount, p->Sbz2 );
  162. // bump over the ACL header to point to the first ACE
  163. pCur += sizeof( ACL );
  164. MySize += sizeof( ACL );
  165. for( i=0; i < p->AceCount; i++ ) {
  166. DumpAce( (PACE_HEADER)pCur, pBase, &Size );
  167. pCur += Size;
  168. MySize += Size;
  169. }
  170. // ACL consistency check
  171. if( p->AclSize != MySize ) {
  172. DbgPrint("Inconsistent ACL Entry! p->AclSize %d, RealSize %d\n",p->AclSize,MySize);
  173. }
  174. // return the size of this ACL
  175. *pSize = MySize;
  176. return;
  177. }
  178. #endif
  179. #if DBG
  180. void
  181. DumpAce(
  182. PACE_HEADER pAce,
  183. PCHAR pBase,
  184. PULONG pSize
  185. )
  186. {
  187. PACE_HEADER p = pAce;
  188. PACCESS_ALLOWED_ACE pAl;
  189. PACCESS_DENIED_ACE pAd;
  190. PSYSTEM_AUDIT_ACE pSa;
  191. PSYSTEM_ALARM_ACE pSl;
  192. PCHAR pTmp;
  193. ULONG MySize, Size;
  194. DbgPrint("ACE_HEADER: Type %d, Flags 0x%x, Size %d\n",
  195. p->AceType, p->AceFlags, p->AceSize );
  196. switch( p->AceType ) {
  197. case ACCESS_ALLOWED_ACE_TYPE:
  198. pAl = (PACCESS_ALLOWED_ACE)p;
  199. DbgPrint("ACCESS_ALLOWED_ACE: AccessMask 0x%x, Sid 0x%x\n",pAl->Mask,pAl->SidStart);
  200. MySize = sizeof(ACCESS_ALLOWED_ACE);
  201. if( pAl->SidStart ) {
  202. pTmp = (PCHAR)&pAl->SidStart;
  203. CtxDumpSid( (PSID)pTmp, pBase, &Size );
  204. MySize += Size;
  205. // Adjust for the first ULONG of the ACE
  206. // being part of the Sid
  207. MySize -= sizeof(ULONG);
  208. }
  209. break;
  210. case ACCESS_DENIED_ACE_TYPE:
  211. pAd = (PACCESS_DENIED_ACE)p;
  212. DbgPrint("ACCESS_DENIED_ACE: AccessMask 0x%x, Sid 0x%x\n",pAd->Mask,pAd->SidStart);
  213. MySize = sizeof(ACCESS_DENIED_ACE);
  214. if( pAd->SidStart ) {
  215. pTmp = (PCHAR)&pAd->SidStart;
  216. CtxDumpSid( (PSID)pTmp, pBase, &Size );
  217. MySize += Size;
  218. // Adjust for the first ULONG of the ACE
  219. // being part of the Sid
  220. MySize -= sizeof(ULONG);
  221. }
  222. break;
  223. case SYSTEM_AUDIT_ACE_TYPE:
  224. pSa = (PSYSTEM_AUDIT_ACE)p;
  225. DbgPrint("SYSTEM_AUDIT_ACE: AccessMask 0x%x, Sid 0x%x\n",pSa->Mask,pSa->SidStart);
  226. MySize = sizeof(SYSTEM_AUDIT_ACE);
  227. if( pSa->SidStart ) {
  228. pTmp = (PCHAR)&pSa->SidStart;
  229. CtxDumpSid( (PSID)pTmp, pBase, &Size );
  230. MySize += Size;
  231. // Adjust for the first ULONG of the ACE
  232. // being part of the Sid
  233. MySize -= sizeof(ULONG);
  234. }
  235. break;
  236. case SYSTEM_ALARM_ACE_TYPE:
  237. pSl = (PSYSTEM_ALARM_ACE)p;
  238. DbgPrint("SYSTEM_ALARM_ACE: AccessMask 0x%x, Sid 0x%x\n",pSl->Mask,pSl->SidStart);
  239. MySize = sizeof(SYSTEM_ALARM_ACE);
  240. if( pSl->SidStart ) {
  241. pTmp = (PCHAR)&pSl->SidStart;
  242. CtxDumpSid( (PSID)pTmp, pBase, &Size );
  243. MySize += Size;
  244. // Adjust for the first ULONG of the ACE
  245. // being part of the Sid
  246. MySize -= sizeof(ULONG);
  247. }
  248. break;
  249. default:
  250. DbgPrint("Unknown ACE type %d\n", p->AceType);
  251. }
  252. // Check its consistency
  253. if( p->AceSize != MySize ) {
  254. DbgPrint("Inconsistent ACE Entry! p->AceSize %d, RealSize %d\n",p->AceSize,MySize);
  255. }
  256. // return the size so the caller can update the pointer
  257. *pSize = p->AceSize;
  258. DbgPrint("\n");
  259. return;
  260. }
  261. #endif
  262.