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.

171 lines
3.9 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. DFSSTATUS
  28. DfsRemoveDisabledPrivileges (void)
  29. {
  30. DFSSTATUS Status = ERROR_SUCCESS;
  31. DWORD BufferSize = 0;
  32. BOOL ProcessOpened = FALSE;
  33. HANDLE hProcessToken = INVALID_HANDLE_VALUE;
  34. PTOKEN_PRIVILEGES pTokenPrivs = NULL;
  35. DWORD i = 0;
  36. #define PRIVILEGE_NAME_LENGTH MAX_PATH
  37. WCHAR PrivilegeName[PRIVILEGE_NAME_LENGTH];
  38. DWORD PrivilegeNameLength = PRIVILEGE_NAME_LENGTH;
  39. //
  40. // Open the token.
  41. //
  42. ProcessOpened = OpenProcessToken(GetCurrentProcess(),
  43. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  44. &hProcessToken);
  45. if (Status != ERROR_SUCCESS)
  46. {
  47. Status = GetLastError();
  48. goto Cleanup;
  49. }
  50. //
  51. // First find out the buffer size we need.
  52. //
  53. GetTokenInformation(hProcessToken,
  54. TokenPrivileges,
  55. NULL,
  56. 0,
  57. &BufferSize
  58. );
  59. //
  60. // Allocate the buffer and get the info
  61. //
  62. pTokenPrivs = (PTOKEN_PRIVILEGES) LocalAlloc(LMEM_FIXED, BufferSize);
  63. if(pTokenPrivs == NULL)
  64. {
  65. Status = GetLastError();
  66. goto Cleanup;
  67. }
  68. if(!GetTokenInformation(hProcessToken,
  69. TokenPrivileges,
  70. pTokenPrivs,
  71. BufferSize,
  72. &BufferSize))
  73. {
  74. Status = GetLastError();
  75. goto Cleanup;
  76. }
  77. //
  78. // Find all non-enabled privileges and mark them for removal
  79. //
  80. for(i=0; i < pTokenPrivs->PrivilegeCount; i++)
  81. {
  82. if(!(pTokenPrivs->Privileges[i].Attributes &
  83. (SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_ENABLED_BY_DEFAULT)))
  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. Cleanup:
  111. if(hProcessToken != INVALID_HANDLE_VALUE)
  112. {
  113. CloseHandle (hProcessToken);
  114. }
  115. if(pTokenPrivs)
  116. {
  117. LocalFree (pTokenPrivs);
  118. }
  119. return Status;
  120. }
  121. DFSSTATUS
  122. DfsEnablePrivilege(ULONG Privilege, LPCTSTR PrivName)
  123. {
  124. NTSTATUS NtStatus = 0;
  125. DFSSTATUS Status = 0;
  126. BOOLEAN WasEnabled = FALSE;
  127. UNREFERENCED_PARAMETER(PrivName);
  128. NtStatus = RtlAdjustPrivilege(Privilege, TRUE, FALSE, &WasEnabled);
  129. if(!NT_SUCCESS(NtStatus))
  130. {
  131. Status = RtlNtStatusToDosError(NtStatus);
  132. }
  133. return Status;
  134. }