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.

176 lines
4.0 KiB

  1. //
  2. // Copyright (C) 2000-2002, Microsoft Corporation
  3. //
  4. // File: Processsecurity.c
  5. //
  6. // Contents: miscellaneous dfs functions.
  7. //
  8. // History: April 16 2002, Author: Rohanp
  9. //
  10. //-----------------------------------------------------------------------------
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <ntseapi.h>
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include <dsgetdc.h>
  20. #include <lm.h>
  21. #include <dfsheader.h>
  22. #include <dfsmisc.h>
  23. #include <shellapi.h>
  24. #include <Aclapi.h>
  25. #include <authz.h>
  26. #include <lm.h>
  27. #include "securitylogmacros.hxx"
  28. DFSSTATUS
  29. DfsRemoveDisabledPrivileges (void)
  30. {
  31. DFSSTATUS Status = ERROR_SUCCESS;
  32. DWORD BufferSize = 0;
  33. BOOL ProcessOpened = FALSE;
  34. HANDLE hProcessToken = INVALID_HANDLE_VALUE;
  35. PTOKEN_PRIVILEGES pTokenPrivs = NULL;
  36. DWORD i = 0;
  37. #define PRIVILEGE_NAME_LENGTH MAX_PATH
  38. WCHAR PrivilegeName[PRIVILEGE_NAME_LENGTH];
  39. DWORD PrivilegeNameLength = PRIVILEGE_NAME_LENGTH;
  40. //
  41. // Open the token.
  42. //
  43. ProcessOpened = OpenProcessToken(GetCurrentProcess(),
  44. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  45. &hProcessToken);
  46. if (Status != ERROR_SUCCESS)
  47. {
  48. Status = GetLastError();
  49. goto Cleanup;
  50. }
  51. //
  52. // First find out the buffer size we need.
  53. //
  54. GetTokenInformation(hProcessToken,
  55. TokenPrivileges,
  56. NULL,
  57. 0,
  58. &BufferSize
  59. );
  60. //
  61. // Allocate the buffer and get the info
  62. //
  63. pTokenPrivs = (PTOKEN_PRIVILEGES) LocalAlloc(LMEM_FIXED, BufferSize);
  64. if(pTokenPrivs == NULL)
  65. {
  66. Status = GetLastError();
  67. goto Cleanup;
  68. }
  69. if(!GetTokenInformation(hProcessToken,
  70. TokenPrivileges,
  71. pTokenPrivs,
  72. BufferSize,
  73. &BufferSize))
  74. {
  75. Status = GetLastError();
  76. goto Cleanup;
  77. }
  78. //
  79. // Find all non-enabled privileges and mark them for removal
  80. //
  81. for(i=0; i < pTokenPrivs->PrivilegeCount; i++)
  82. {
  83. if(!(pTokenPrivs->Privileges[i].Attributes & SE_PRIVILEGE_ENABLED))
  84. {
  85. pTokenPrivs->Privileges[i].Attributes = SE_PRIVILEGE_REMOVED;
  86. if(!LookupPrivilegeName(NULL,
  87. &(pTokenPrivs->Privileges[i].Luid),
  88. PrivilegeName,
  89. &PrivilegeNameLength))
  90. {
  91. }
  92. else
  93. {
  94. }
  95. }
  96. }
  97. //
  98. // Now, actually remove the privileges
  99. //
  100. if(!AdjustTokenPrivileges(hProcessToken,
  101. FALSE,
  102. pTokenPrivs,
  103. BufferSize,
  104. NULL,
  105. NULL))
  106. {
  107. Status = GetLastError();
  108. goto Cleanup;
  109. }
  110. Status = GetLastError();
  111. if(Status == ERROR_NOT_ALL_ASSIGNED)
  112. {
  113. goto Cleanup;
  114. }
  115. Cleanup:
  116. if(hProcessToken != INVALID_HANDLE_VALUE)
  117. {
  118. CloseHandle (hProcessToken);
  119. }
  120. if(pTokenPrivs)
  121. {
  122. LocalFree (pTokenPrivs);
  123. }
  124. return Status;
  125. }
  126. DFSSTATUS
  127. DfsAdjustPrivilege(ULONG Privilege,
  128. BOOLEAN bEnable)
  129. {
  130. NTSTATUS NtStatus = 0;
  131. DFSSTATUS Status = 0;
  132. BOOLEAN WasEnabled = FALSE;
  133. NtStatus = RtlAdjustPrivilege(Privilege, bEnable, FALSE, &WasEnabled);
  134. if(!NT_SUCCESS(NtStatus))
  135. {
  136. Status = RtlNtStatusToDosError(NtStatus);
  137. }
  138. return Status;
  139. }