Leaked source code of windows server 2003
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.

537 lines
16 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1998.
  5. //
  6. // File: Main.cxx
  7. //
  8. // Contents: Main file for CI security dump utility
  9. //
  10. // History: 29-Jul-1998 KyleP Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <stdio.h>
  14. #include <windows.h>
  15. #include <aclapi.h>
  16. typedef ULONG SDID;
  17. //
  18. // Copied from SecCache.hxx (NtCiUtil directory)
  19. //
  20. const USHORT SECSTORE_REC_SIZE = 64;
  21. const ULONG SECSTORE_HASH_SIZE = 199;
  22. struct SSdHeaderRecord
  23. {
  24. ULONG cbSD; // size in bytes of the security descriptor
  25. ULONG ulHash; // the hash of the security descriptor
  26. SDID iHashChain; // index to previous entry for hash bucket
  27. };
  28. //
  29. // Used for mapping bitmasks to text.
  30. //
  31. struct SPermDisplay
  32. {
  33. DWORD Perm;
  34. char * Display;
  35. };
  36. //
  37. // Local constants and function prototypes
  38. //
  39. unsigned const SixtyFourK = 1024 * 64;
  40. void DisplayTrustee( TRUSTEE const & Trustee );
  41. void DisplayACE( char const * pszPreface, unsigned cACE, EXPLICIT_ACCESS * pACE );
  42. void DisplayMode( DWORD mode );
  43. void DisplayInheritance( DWORD Inherit );
  44. void DisplayPerms( DWORD grfAccess );
  45. void Display( DWORD grfAccess, SPermDisplay aPerm[], unsigned cPerm, unsigned cDisplay = 0 );
  46. void Usage();
  47. //+---------------------------------------------------------------------------
  48. //
  49. // Function: wmain, public
  50. //
  51. // Synopsis: Program entry point. Iterates and displays SDID mapping.
  52. //
  53. // Arguments: [argc] -- Argument count
  54. // [argv] -- Program arguments
  55. //
  56. // History: 29-Jul-1998 KyleP Created
  57. //
  58. //----------------------------------------------------------------------------
  59. extern "C" int __cdecl wmain( int argc, WCHAR * argv[] )
  60. {
  61. if ( argc != 2 )
  62. {
  63. Usage();
  64. return 1;
  65. }
  66. //
  67. // Open handle
  68. //
  69. if ( wcslen( argv[1] ) > ( MAX_PATH - 20 ) )
  70. {
  71. Usage();
  72. return 1;
  73. }
  74. WCHAR wszSecFile[MAX_PATH];
  75. wcscpy( wszSecFile, argv[1] );
  76. wcscat( wszSecFile, L"\\CiST0000.001" );
  77. HANDLE h = CreateFile( wszSecFile,
  78. GENERIC_READ,
  79. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  80. 0,
  81. OPEN_EXISTING,
  82. 0,
  83. 0 );
  84. if ( INVALID_HANDLE_VALUE == h )
  85. {
  86. printf( "Can't open file %ws. Error %u\n", wszSecFile, GetLastError() );
  87. return GetLastError();
  88. }
  89. //
  90. // Read until done.
  91. //
  92. BYTE abTemp[SixtyFourK];
  93. DWORD cbRead;
  94. int i = 0;
  95. SSdHeaderRecord Header;
  96. while ( ReadFile( h,
  97. &Header,
  98. sizeof(Header),
  99. &cbRead,
  100. 0 ) )
  101. {
  102. if ( 0 == Header.cbSD )
  103. break;
  104. i++;
  105. printf( "SDID %u / 0x%x (cbSD = %u bytes)\n", i, i, Header.cbSD );
  106. //
  107. // Read rest of first record.
  108. //
  109. if ( !ReadFile( h,
  110. abTemp,
  111. SECSTORE_REC_SIZE - sizeof(Header) + 4,
  112. &cbRead,
  113. 0 ) )
  114. {
  115. printf( "Error %u reading file\n", GetLastError() );
  116. return 1;
  117. }
  118. //
  119. // Read additional records, which together create one security descriptor
  120. //
  121. if ( Header.cbSD > (SECSTORE_REC_SIZE - sizeof(Header)) )
  122. {
  123. unsigned iCurrent = SECSTORE_REC_SIZE - sizeof(Header);
  124. for ( unsigned cLeft = (Header.cbSD - SECSTORE_REC_SIZE + sizeof(Header) - 1) / SECSTORE_REC_SIZE + 1;
  125. cLeft > 0;
  126. cLeft-- )
  127. {
  128. if ( !ReadFile( h,
  129. abTemp + iCurrent,
  130. SECSTORE_REC_SIZE + 4,
  131. &cbRead,
  132. 0 ) )
  133. {
  134. printf( "Error %u reading file\n", GetLastError() );
  135. return 1;
  136. }
  137. i++;
  138. iCurrent += SECSTORE_REC_SIZE;
  139. }
  140. }
  141. SECURITY_DESCRIPTOR * pSD = (SECURITY_DESCRIPTOR *)abTemp;
  142. //
  143. // Create a human-readable descriptor
  144. //
  145. TRUSTEE * pOwner = 0;
  146. TRUSTEE * pGroup = 0;
  147. DWORD cACE;
  148. EXPLICIT_ACCESS * pACE;
  149. DWORD cAudit = 0;
  150. EXPLICIT_ACCESS * pAudit = 0;
  151. DWORD dwError = LookupSecurityDescriptorParts( &pOwner,
  152. &pGroup,
  153. &cACE,
  154. &pACE,
  155. &cAudit,
  156. &pAudit,
  157. pSD );
  158. //
  159. // And display it.
  160. //
  161. if ( ERROR_SUCCESS == dwError )
  162. {
  163. if ( 0 != pOwner )
  164. {
  165. printf( "Owner: " );
  166. DisplayTrustee( *pOwner );
  167. printf( "\n" );
  168. LocalFree( pOwner );
  169. }
  170. if ( 0 != pGroup )
  171. {
  172. printf( "Group: " );
  173. DisplayTrustee( *pGroup );
  174. printf( "\n" );
  175. LocalFree( pGroup );
  176. }
  177. if ( cACE > 0 )
  178. {
  179. printf( "Access: " );
  180. DisplayACE( " ", cACE, pACE );
  181. printf( "\n" );
  182. LocalFree( pACE );
  183. }
  184. #if 0 // comes out the same as the access
  185. if ( cAudit > 0 )
  186. {
  187. printf( "Audit: " );
  188. DisplayACE( " ", cAudit, pAudit );
  189. printf( "\n" );
  190. LocalFree( pAudit );
  191. }
  192. #endif
  193. }
  194. else
  195. printf( "LookupSecurityDescriptorParts returned %u\n", dwError );
  196. printf( "\n\n" );
  197. }
  198. CloseHandle( h );
  199. return 0;
  200. }
  201. //+---------------------------------------------------------------------------
  202. //
  203. // Function: DisplayTrustee
  204. //
  205. // Synopsis: Prints out trustee (user, group, etc.)
  206. //
  207. // Arguments: [Trustee] -- Trustee description
  208. //
  209. // History: 29-Jul-1998 KyleP Created
  210. //
  211. //----------------------------------------------------------------------------
  212. char * aszTrusteeType[] = { "Unknown", // TRUSTEE_IS_UNKNOWN
  213. "User", // TRUSTEE_IS_USER,
  214. "Group", // TRUSTEE_IS_GROUP,
  215. "Domain", // TRUSTEE_IS_DOMAIN,
  216. "Alias", // TRUSTEE_IS_ALIAS,
  217. "Group", // TRUSTEE_IS_WELL_KNOWN_GROUP,
  218. "Deleted", // TRUSTEE_IS_DELETED,
  219. "Invalid", // TRUSTEE_IS_INVALID
  220. "Unknown",
  221. "Computer" };
  222. void DisplayTrustee( TRUSTEE const & Trustee )
  223. {
  224. if ( TRUSTEE_IS_NAME == Trustee.TrusteeForm )
  225. {
  226. printf( "%ws (%s)", Trustee.ptstrName, aszTrusteeType[Trustee.TrusteeType] );
  227. }
  228. else if ( TRUSTEE_IS_SID == Trustee.TrusteeForm )
  229. {
  230. WCHAR wszName[100];
  231. DWORD ccName = sizeof(wszName)/sizeof(wszName[0]);
  232. WCHAR wszDomain[100];
  233. DWORD ccDomain = sizeof(wszDomain)/sizeof(wszDomain[0]);
  234. SID_NAME_USE snu;
  235. BOOL fOk = LookupAccountSid( 0, // local system
  236. (PSID)Trustee.ptstrName, // address of security identifier
  237. wszName, // address of string for account name
  238. &ccName, // address of size account string
  239. wszDomain, // address of string for referenced domain
  240. &ccDomain, // address of size domain string
  241. &snu ); // address of structure for SID type
  242. if ( fOk )
  243. {
  244. if ( 0 == wszDomain[0] )
  245. printf( "%ws (%s)", wszName, aszTrusteeType[snu] );
  246. else
  247. printf( "%ws\\%ws (%s)", wszDomain, wszName, aszTrusteeType[snu] );
  248. }
  249. else
  250. printf( "<SID>" );
  251. }
  252. else
  253. printf( "Invalid Trustee form: %d\n", Trustee.TrusteeForm );
  254. }
  255. //+---------------------------------------------------------------------------
  256. //
  257. // Function: DisplayACE
  258. //
  259. // Synopsis: Prints out Access Control Entry(ies)
  260. //
  261. // Arguments: [pszPreface] -- String to append at beginning of each line.
  262. // [cACE] -- Count of entries
  263. // [pACE] -- Array of entries
  264. //
  265. // History: 29-Jul-1998 KyleP Created
  266. //
  267. //----------------------------------------------------------------------------
  268. void DisplayACE( char const * pszPreface, unsigned cACE, EXPLICIT_ACCESS * pACE )
  269. {
  270. for ( unsigned i = 0; i < cACE; i++ )
  271. {
  272. if ( 0 != i )
  273. printf( "%s", pszPreface );
  274. DisplayTrustee( pACE[i].Trustee );
  275. printf( " : " );
  276. DisplayMode( pACE[i].grfAccessMode );
  277. printf( " /" );
  278. DisplayInheritance( pACE[i].grfInheritance );
  279. printf( " /" );
  280. DisplayPerms( pACE[i].grfAccessPermissions );
  281. printf( "\n" );
  282. }
  283. //ACCESS_MODE grfAccessMode; DWORD grfInheritance;
  284. }
  285. //+---------------------------------------------------------------------------
  286. //
  287. // Function: DisplayMode, private
  288. //
  289. // Synopsis: Prints out access mode (Set or Deny access)
  290. //
  291. // Arguments: [mode] -- Access mode
  292. //
  293. // History: 29-Jul-1998 KyleP Created
  294. //
  295. //----------------------------------------------------------------------------
  296. char * aszAccessDisplay[] = { "NOT_USED",
  297. "GRANT_ACCESS",
  298. "SET_ACCESS",
  299. "DENY_ACCESS",
  300. "REVOKE_ACCESS",
  301. "SET_AUDIT_SUCCESS",
  302. "SET_AUDIT_FAILURE" };
  303. void DisplayMode( DWORD mode )
  304. {
  305. printf( "%s", aszAccessDisplay[mode] );
  306. }
  307. //+---------------------------------------------------------------------------
  308. //
  309. // Function: DisplayInheritance, private
  310. //
  311. // Synopsis: Prints out inheritance, both up (to parent) and down (to children)
  312. //
  313. // Arguments: [Inherit] -- Inheritance bitmask
  314. //
  315. // History: 29-Jul-1998 KyleP Created
  316. //
  317. //----------------------------------------------------------------------------
  318. SPermDisplay aInheritDisplay[] = {
  319. //{ INHERITED_ACCESS_ENTRY, "(inherited)" },
  320. { INHERITED_PARENT, "(inherited from parent)" },
  321. { INHERITED_GRANDPARENT, "(inherited from grandparent)" },
  322. { SUB_OBJECTS_ONLY_INHERIT, "SUB_OBJECTS_ONLY" },
  323. { SUB_CONTAINERS_ONLY_INHERIT, "SUB_CONTAINERS_ONLY" },
  324. { SUB_CONTAINERS_AND_OBJECTS_INHERIT, "SUB_CONTAINERS_AND_OBJECTS" },
  325. { INHERIT_NO_PROPAGATE, "INHERIT_NO_PROPAGATE" },
  326. { INHERIT_ONLY, "INHERIT_ONLY" } };
  327. void DisplayInheritance( DWORD Inherit )
  328. {
  329. if ( NO_INHERITANCE == Inherit )
  330. printf( "\n\t\t(not inherited)" );
  331. else
  332. Display( Inherit, aInheritDisplay, sizeof(aInheritDisplay)/sizeof(aInheritDisplay[0]) );
  333. }
  334. //+---------------------------------------------------------------------------
  335. //
  336. // Function: DisplayPerms
  337. //
  338. // Synopsis: Displays file permissions
  339. //
  340. // Arguments: [grfAccess] -- Access permission bitmask
  341. //
  342. // History: 29-Jul-1998 KyleP Created
  343. //
  344. //----------------------------------------------------------------------------
  345. SPermDisplay aPermDisplay[] = {
  346. { FILE_READ_DATA, "READ_DATA" },
  347. { FILE_WRITE_DATA, "WRITE_DATA" },
  348. { FILE_ADD_FILE, "ADD_FILE" },
  349. { FILE_APPEND_DATA, "APPEND_DATA" },
  350. { FILE_ADD_SUBDIRECTORY, "ADD_SUBDIRECTORY" },
  351. { FILE_CREATE_PIPE_INSTANCE, "CREATE_PIPE_INSTANCE" },
  352. { FILE_READ_EA, "READ_EA" },
  353. { FILE_WRITE_EA, "WRITE_EA" },
  354. { FILE_EXECUTE, "EXECUTE" },
  355. { FILE_TRAVERSE, "TRAVERSE" },
  356. { FILE_DELETE_CHILD, "DELETE_CHILD" },
  357. { FILE_READ_ATTRIBUTES, "READ_ATTRIBUTES" },
  358. { FILE_WRITE_ATTRIBUTES, "WRITE_ATTRIBUTES" },
  359. { DELETE, "DELETE" },
  360. { READ_CONTROL, "READ_CONTROL" },
  361. { WRITE_DAC, "WRITE_DAC" },
  362. { WRITE_OWNER, "WRITE_OWNER" },
  363. { SYNCHRONIZE, "SYNCHRONIZE" },
  364. { GENERIC_READ, "GENERIC_READ" },
  365. { GENERIC_WRITE, "GENERIC_WRITE" },
  366. { GENERIC_EXECUTE, "GENERIC_EXECUTE" } };
  367. void DisplayPerms( DWORD grfAccess )
  368. {
  369. BOOL cDisplay = 0;
  370. DWORD grfRemove = 0;
  371. printf( "\n\t\t" );
  372. //
  373. // First, get rid of the basics...
  374. //
  375. if ( (grfAccess & FILE_GENERIC_READ) == FILE_GENERIC_READ )
  376. {
  377. printf( "GENERIC_READ" );
  378. grfRemove = FILE_GENERIC_READ;
  379. cDisplay++;
  380. }
  381. if ( (grfAccess & FILE_GENERIC_WRITE) == FILE_GENERIC_WRITE )
  382. {
  383. if ( 0 != cDisplay )
  384. printf( " | " );
  385. printf( "GENERIC_WRITE" );
  386. grfRemove = grfRemove | FILE_GENERIC_WRITE;
  387. cDisplay++;
  388. }
  389. if ( (grfAccess & FILE_GENERIC_EXECUTE) == FILE_GENERIC_EXECUTE )
  390. {
  391. if ( 0 != cDisplay )
  392. printf( " | " );
  393. if ( 0 == (cDisplay % 2) )
  394. printf( " \n\t\t" );
  395. printf( "GENERIC_EXECUTE" );
  396. grfRemove = grfRemove | FILE_GENERIC_EXECUTE;
  397. cDisplay++;
  398. }
  399. //
  400. // Now, individual permissions.
  401. //
  402. DWORD grfRemainder = grfAccess & ~grfRemove;
  403. Display( grfRemainder, aPermDisplay, sizeof(aPermDisplay)/sizeof(aPermDisplay[0]), cDisplay );
  404. printf( " (0x%x)", grfAccess );
  405. }
  406. //+---------------------------------------------------------------------------
  407. //
  408. // Function: Display, private
  409. //
  410. // Synopsis: Print bit masks
  411. //
  412. // Arguments: [grfAccess] -- Bit mask
  413. // [aPerm] -- Description of bits
  414. // [cPerm] -- Count of entries in [aPerm]
  415. // [cDisplay] -- Number of entries already displayed on
  416. // current line by caller.
  417. //
  418. // History: 29-Jul-1998 KyleP Created
  419. //
  420. //----------------------------------------------------------------------------
  421. void Display( DWORD grfAccess, SPermDisplay aPerm[], unsigned cPerm, unsigned cDisplay )
  422. {
  423. for ( unsigned i = 0; i < cPerm ; i++ )
  424. {
  425. if ( grfAccess & aPerm[i].Perm )
  426. {
  427. if ( 0 != cDisplay )
  428. printf( " | " );
  429. if ( 0 == (cDisplay % 2) )
  430. printf( " \n\t\t" );
  431. printf( "%s", aPerm[i].Display );
  432. cDisplay++;
  433. }
  434. }
  435. }
  436. //+---------------------------------------------------------------------------
  437. //
  438. // Function: Usage
  439. //
  440. // Synopsis: Displays program usage
  441. //
  442. // History: 29-Jul-1998 KyleP Created
  443. //
  444. //----------------------------------------------------------------------------
  445. void Usage()
  446. {
  447. printf( "Usage: DumpSec <Path to catalog>\n" );
  448. }