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.

104 lines
3.1 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <winsafer.h>
  5. /*
  6. BOOL WINAPI
  7. CodeAuthzCompareTokenLevels (
  8. IN HANDLE ClientAccessToken,
  9. IN HANDLE ServerAccessToken,
  10. OUT PDWORD pdwResult
  11. )
  12. */
  13. void _cdecl main()
  14. {
  15. static const levelids[4] = {
  16. AUTHZLEVELID_UNTRUSTED,
  17. AUTHZLEVELID_CONSTRAINED,
  18. AUTHZLEVELID_NORMALUSER,
  19. AUTHZLEVELID_FULLYTRUSTED
  20. };
  21. HANDLE hTokens[4];
  22. BOOL bStatus;
  23. DWORD i;
  24. HANDLE hProcessToken;
  25. bStatus = OpenProcessToken(GetCurrentProcess(),
  26. TOKEN_QUERY | TOKEN_DUPLICATE,
  27. &hProcessToken);
  28. if (!bStatus) {
  29. printf("Failed to open process token (lasterror=%d).\n", GetLastError());
  30. return;
  31. }
  32. for (int i = 0; i < 4; i++)
  33. {
  34. HAUTHZLEVEL hCodeAuthLevel;
  35. bStatus = CreateCodeAuthzLevel(AUTHZSCOPEID_MACHINE,
  36. levelids[i],
  37. AUTHZCRLEV_OPEN,
  38. &hCodeAuthLevel,
  39. NULL);
  40. if (!bStatus) {
  41. printf("Failed to create level %d (lasterror=%d)\n", levelids[i], GetLastError());
  42. return;
  43. }
  44. bStatus = ComputeAccessTokenFromCodeAuthzLevel(hCodeAuthLevel,
  45. hProcessToken,
  46. &hTokens[i],
  47. 0,
  48. NULL);
  49. if (!bStatus) {
  50. printf("ComputeAccessTokenFromCodeAuthzLevel failed with GLE=%d\n", GetLastError());
  51. return;
  52. }
  53. bStatus = CloseCodeAuthzLevel(hCodeAuthLevel);
  54. if (!bStatus) {
  55. printf("Failed to close level.\n");
  56. return;
  57. }
  58. }
  59. for (int testi = 0; testi < 4; testi++) {
  60. for (int testj = 0; testj < 4; testj++) {
  61. DWORD dwCompareResults;
  62. DWORD dwExpected;
  63. bStatus = CodeAuthzCompareTokenLevels (
  64. hTokens[testi],
  65. hTokens[testj],
  66. &dwCompareResults);
  67. if (!bStatus) {
  68. printf("CompareTokens failed for test %d,%d with error=%d\n",
  69. testi, testj, GetLastError());
  70. continue;
  71. }
  72. if (testi == testj) {
  73. dwExpected = 0;
  74. } else if (testi < testj) {
  75. dwExpected = 1;
  76. } else {
  77. dwExpected = -1;
  78. }
  79. if (dwCompareResults != dwExpected) {
  80. printf("CompareTokens return wrong value for test %d,%d (actual=%d, expected=%d)\n",
  81. testi, testj, dwCompareResults, dwExpected);
  82. } else {
  83. printf("CompareTokens passed test %d,%d (returned %d)\n",
  84. testi, testj, dwCompareResults);
  85. }
  86. }
  87. }
  88. return;
  89. }