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.

278 lines
7.3 KiB

  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. #include <Windows.h>
  3. #include <TChar.h>
  4. #include <MsgFile.h>
  5. #include <TelnetD.h>
  6. #include <RegUtil.h>
  7. #include <TlntUtils.h>
  8. #include <Debug.h>
  9. #include <KillApps.h>
  10. #include <psapi.h>
  11. #pragma warning(disable:4100)
  12. #pragma warning(disable: 4127)
  13. using namespace _Utils;
  14. using CDebugLevel::TRACE_DEBUGGING;
  15. using CDebugLevel::TRACE_HANDLE;
  16. using CDebugLevel::TRACE_SOCKET;
  17. #define DOWN_WITH_AUTHORITY {0, 0, 0, 0, 0x6, 0x66} // s-1-666
  18. #define DEMONS 1
  19. PSID g_psidBgJobGroup = NULL;
  20. DWORD g_dwKillAllApps = DEFAULT_DISCONNECT_KILLALL_APPS;
  21. extern HANDLE g_hSyncCloseHandle;
  22. bool CreateBgjobSpecificSid()
  23. {
  24. SID_IDENTIFIER_AUTHORITY AnarchyAuthority = DOWN_WITH_AUTHORITY;
  25. if( !AllocateAndInitializeSid( &AnarchyAuthority, 1, DEMONS,
  26. 0, 0, 0, 0, 0, 0, 0, &g_psidBgJobGroup ) )
  27. {
  28. return false;
  29. }
  30. return true;
  31. }
  32. bool IsAclAddedByBgJobPresent( PACL pAcl )
  33. {
  34. ACCESS_DENIED_ACE *pAce = NULL;
  35. WORD wIndex = 0;
  36. for( wIndex=0; wIndex<pAcl->AceCount; wIndex++ )
  37. {
  38. if( GetAce( pAcl, wIndex, ( PVOID * )&pAce ) )
  39. {
  40. if( EqualSid( g_psidBgJobGroup, &( pAce->SidStart ) ) )
  41. {
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. //We check if this process's DACL has an ACE added by the BgJob
  49. //ACE is generated from a SID know both to the BgJob and tlntsess.exe
  50. bool IsThisProcessLaunchedFromBgJob( HANDLE hToken )
  51. {
  52. DWORD dwLength = 0;
  53. TOKEN_DEFAULT_DACL *ptdDacl = NULL;
  54. if( g_dwKillAllApps )
  55. {
  56. return false;
  57. }
  58. // Get required buffer size and allocate the Default Dacl buffer.
  59. if (!GetTokenInformation( hToken, TokenDefaultDacl, NULL, 0, &dwLength ) )
  60. {
  61. if(GetLastError() != ERROR_INSUFFICIENT_BUFFER )
  62. return false;
  63. ptdDacl = ( TOKEN_DEFAULT_DACL * ) HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);
  64. }
  65. if ( ptdDacl == NULL)
  66. {
  67. return false;
  68. }
  69. if ( GetTokenInformation( hToken, TokenDefaultDacl, ptdDacl, dwLength, &dwLength ) )
  70. {
  71. if( ptdDacl && IsAclAddedByBgJobPresent( ptdDacl->DefaultDacl ) )
  72. {
  73. HeapFree( GetProcessHeap(), 0,ptdDacl );
  74. return ( true );
  75. }
  76. }
  77. HeapFree( GetProcessHeap(), 0,ptdDacl );
  78. return( false );
  79. }
  80. void EnumSessionProcesses( LUID id, void fPtr ( HANDLE, DWORD, LPWSTR ),
  81. ENUM_PURPOSE epWhyEnumerate )
  82. {
  83. DWORD rgdwPids[ MAX_PROCESSES_IN_SYSTEM ];
  84. DWORD dwActualSizeInBytes = 0;
  85. DWORD dwActualNoOfPids = 0;
  86. DWORD dwIndex = 0;
  87. HANDLE hProc = NULL;
  88. HANDLE hAccessToken = NULL;
  89. LUID luidID;
  90. EnableDebugPriv();
  91. EnumProcesses( rgdwPids, MAX_PROCESSES_IN_SYSTEM, &dwActualSizeInBytes );
  92. dwActualNoOfPids = dwActualSizeInBytes / sizeof( DWORD );
  93. for( dwIndex = 0; dwIndex < dwActualNoOfPids; dwIndex++ )
  94. {
  95. SfuZeroMemory( &luidID, sizeof( luidID ) );
  96. hProc = OpenProcess( PROCESS_ALL_ACCESS, FALSE, rgdwPids[ dwIndex ] );
  97. if( hProc )
  98. {
  99. if( OpenProcessToken( hProc, TOKEN_QUERY, &hAccessToken ))
  100. {
  101. if( GetAuthenticationId( hAccessToken, &luidID ) )
  102. {
  103. if( id.HighPart == luidID.HighPart&&
  104. id.LowPart == luidID.LowPart )
  105. {
  106. //this process belongs to our session
  107. if( epWhyEnumerate != TO_CLEANUP ||
  108. !IsThisProcessLaunchedFromBgJob( hAccessToken ) )
  109. {
  110. LPTSTR lpszProcessName = NULL;
  111. ( fPtr )( hProc, rgdwPids[ dwIndex ], lpszProcessName );
  112. _TRACE( TRACE_DEBUGGING, " pid = %d ", rgdwPids[ dwIndex ] );
  113. }
  114. }
  115. }
  116. TELNET_CLOSE_HANDLE( hAccessToken );
  117. }
  118. TELNET_CLOSE_HANDLE( hProc );
  119. }
  120. }
  121. }
  122. BOOL EnableDebugPriv( VOID )
  123. {
  124. HANDLE hToken;
  125. LUID DebugValue;
  126. TOKEN_PRIVILEGES tkp;
  127. //
  128. // Retrieve a handle of the access token
  129. //
  130. if (!OpenProcessToken(GetCurrentProcess(),
  131. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  132. &hToken))
  133. {
  134. // printf("OpenProcessToken failed with %d\n", GetLastError());
  135. return FALSE;
  136. }
  137. //
  138. // Enable the SE_DEBUG_NAME privilege or disable
  139. // all privileges, depending on the fEnable flag.
  140. //
  141. if (!LookupPrivilegeValue((LPTSTR) NULL,
  142. SE_DEBUG_NAME,
  143. &DebugValue))
  144. {
  145. TELNET_CLOSE_HANDLE( hToken );
  146. // printf("LookupPrivilegeValue failed with %d\n", GetLastError());
  147. return FALSE;
  148. }
  149. tkp.PrivilegeCount = 1;
  150. tkp.Privileges[0].Luid = DebugValue;
  151. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  152. if (!AdjustTokenPrivileges(
  153. hToken,
  154. FALSE,
  155. &tkp,
  156. sizeof(TOKEN_PRIVILEGES),
  157. (PTOKEN_PRIVILEGES) NULL,
  158. (PDWORD) NULL))
  159. {
  160. TELNET_CLOSE_HANDLE( hToken );
  161. // printf("AdjustTokenPrivileges failed with %d\n", GetLastError());
  162. return FALSE;
  163. }
  164. TELNET_CLOSE_HANDLE( hToken );
  165. return TRUE;
  166. }
  167. BOOL GetAuthenticationId( HANDLE hToken, LUID* pId )
  168. {
  169. BOOL bSuccess = FALSE;
  170. DWORD dwLength = 0;
  171. PTOKEN_STATISTICS pts = NULL;
  172. // Get required buffer size and allocate the TOKEN_GROUPS buffer.
  173. if (!GetTokenInformation( hToken, TokenStatistics, (LPVOID) pts, 0,
  174. &dwLength ))
  175. {
  176. if(GetLastError() != ERROR_INSUFFICIENT_BUFFER )
  177. goto Cleanup;
  178. pts = (PTOKEN_STATISTICS) VirtualAlloc(NULL,dwLength,
  179. MEM_COMMIT, PAGE_READWRITE);
  180. }
  181. if( pts == NULL )
  182. goto Cleanup;
  183. // Get the token group information from the access token.
  184. if( !GetTokenInformation( hToken, TokenStatistics, (LPVOID) pts, dwLength,
  185. &dwLength ))
  186. goto Cleanup;
  187. *pId = pts->AuthenticationId;
  188. bSuccess = TRUE;
  189. Cleanup:
  190. // Free the buffer for the token groups.
  191. if( pts != NULL )
  192. VirtualFree( pts, 0, MEM_RELEASE );
  193. return bSuccess;
  194. }
  195. void KillTheProcess( HANDLE hProc, DWORD dwProcessId, LPWSTR lpszProcessName )
  196. {
  197. TerminateProcess( hProc, 1 );
  198. return;
  199. }
  200. bool GetRegValues()
  201. {
  202. HKEY hk = NULL;
  203. bool bRetVal = false;
  204. if( RegOpenKey( HKEY_LOCAL_MACHINE, REG_PARAMS_KEY, &hk ) )
  205. {
  206. goto ExitOnError;
  207. }
  208. if( !GetRegistryDW( hk, NULL, L"DisconnectKillAllApps", &g_dwKillAllApps,
  209. DEFAULT_DISCONNECT_KILLALL_APPS,FALSE ) )
  210. {
  211. goto ExitOnError;
  212. }
  213. bRetVal = true;
  214. ExitOnError:
  215. RegCloseKey( hk );
  216. return ( bRetVal );
  217. }
  218. BOOL KillProcs( LUID id )
  219. {
  220. GetRegValues();
  221. CreateBgjobSpecificSid();
  222. EnumSessionProcesses( id, KillTheProcess, TO_CLEANUP );
  223. FreeSid( g_psidBgJobGroup );
  224. return TRUE;
  225. }