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.

374 lines
9.4 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: killsvr.c
  6. * Content: kill dplay.exe
  7. * History:
  8. * Date By Reason
  9. * ==== == ======
  10. * 06-apr-95 craige initial implementation
  11. * 24-jun-95 craige kill all attached processes
  12. * 2-feb-97 andyco ported for dplaysvr.exe
  13. * 7-jul-97 kipo added non-console support
  14. *
  15. ***************************************************************************/
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include "dplaysvr.h"
  20. // only do printf's when built as a console app
  21. #ifdef NOCONSOLE
  22. #pragma warning(disable:4002)
  23. #define printf()
  24. #endif
  25. //**********************************************************************
  26. // Globals
  27. //**********************************************************************
  28. BOOL g_fDaclInited = FALSE;
  29. SECURITY_ATTRIBUTES g_sa;
  30. BYTE g_abSD[SECURITY_DESCRIPTOR_MIN_LENGTH];
  31. PSECURITY_ATTRIBUTES g_psa = NULL;
  32. PACL g_pEveryoneACL = NULL;
  33. //**********************************************************************
  34. // ------------------------------
  35. // DNGetNullDacl - Get a SECURITY_ATTRIBUTE structure that specifies a
  36. // NULL DACL which is accessible by all users.
  37. // Taken from IDirectPlay8 code base.
  38. //
  39. // Entry: Nothing
  40. //
  41. // Exit: PSECURITY_ATTRIBUTES
  42. // ------------------------------
  43. #undef DPF_MODNAME
  44. #define DPF_MODNAME "DNGetNullDacl"
  45. PSECURITY_ATTRIBUTES DNGetNullDacl()
  46. {
  47. PSID psidEveryone = NULL;
  48. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY;
  49. DWORD dwAclSize;
  50. // This is done to make this function independent of DNOSIndirectionInit so that the debug
  51. // layer can call it before the indirection layer is initialized.
  52. if (!g_fDaclInited)
  53. {
  54. if (!InitializeSecurityDescriptor((SECURITY_DESCRIPTOR*)g_abSD, SECURITY_DESCRIPTOR_REVISION))
  55. {
  56. DPF(0, "Failed to initialize security descriptor" );
  57. goto Error;
  58. }
  59. // Create SID for the Everyone group.
  60. if (!AllocateAndInitializeSid(&siaWorld, 1, SECURITY_WORLD_RID, 0,
  61. 0, 0, 0, 0, 0, 0, &psidEveryone))
  62. {
  63. DPF(0, "Failed to allocate Everyone SID" );
  64. goto Error;
  65. }
  66. dwAclSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidEveryone) - sizeof(DWORD);
  67. // Allocate the ACL, this won't be a tracked allocation and we will let process cleanup destroy it
  68. g_pEveryoneACL = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
  69. if (g_pEveryoneACL == NULL)
  70. {
  71. DPF(0, "Failed to allocate ACL buffer" );
  72. goto Error;
  73. }
  74. // Intialize the ACL.
  75. if (!InitializeAcl(g_pEveryoneACL, dwAclSize, ACL_REVISION))
  76. {
  77. DPF(0, "Failed to initialize ACL" );
  78. goto Error;
  79. }
  80. // Add the ACE.
  81. if (!AddAccessAllowedAce(g_pEveryoneACL, ACL_REVISION, GENERIC_ALL, psidEveryone))
  82. {
  83. DPF(0, "Failed to add ACE to ACL" );
  84. goto Error;
  85. }
  86. // We no longer need the SID that was allocated.
  87. FreeSid(psidEveryone);
  88. psidEveryone = NULL;
  89. // Add the ACL to the security descriptor..
  90. if (!SetSecurityDescriptorDacl((SECURITY_DESCRIPTOR*)g_abSD, TRUE, g_pEveryoneACL, FALSE))
  91. {
  92. DPF(0, "Failed to add ACL to security descriptor" );
  93. goto Error;
  94. }
  95. g_sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  96. g_sa.lpSecurityDescriptor = g_abSD;
  97. g_sa.bInheritHandle = FALSE;
  98. g_psa = &g_sa;
  99. g_fDaclInited = TRUE;
  100. }
  101. Error:
  102. if (psidEveryone)
  103. {
  104. FreeSid(psidEveryone);
  105. psidEveryone = NULL;
  106. }
  107. return g_psa;
  108. }
  109. //**********************************************************************
  110. /*
  111. * sendRequest
  112. *
  113. * communicate a request to DPHELP
  114. */
  115. static BOOL sendRequest( LPDPHELPDATA req_phd )
  116. {
  117. OSVERSIONINFOA VersionInfo;
  118. BOOL fUseGlobalNamespace;
  119. LPDPHELPDATA phd;
  120. HANDLE hmem;
  121. HANDLE hmutex;
  122. HANDLE hackevent;
  123. HANDLE hstartevent;
  124. BOOL rc;
  125. // Determine if we're running on NT.
  126. memset(&VersionInfo, 0, sizeof(VersionInfo));
  127. VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
  128. if (GetVersionExA(&VersionInfo))
  129. {
  130. if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  131. {
  132. /*
  133. printf("Running on NT version %u.%u.%u, using global namespace.\n",
  134. VersionInfo.dwMajorVersion, VersionInfo.dwMinorVersion, VersionInfo.dwBuildNumber);
  135. */
  136. fUseGlobalNamespace = TRUE;
  137. }
  138. else
  139. {
  140. /*
  141. printf("Running on 9x version %u.%u.%u, not using global namespace.\n",
  142. VersionInfo.dwMajorVersion, VersionInfo.dwMinorVersion, LOWORD(VersionInfo.dwBuildNumber));
  143. */
  144. fUseGlobalNamespace = FALSE;
  145. }
  146. }
  147. else
  148. {
  149. //printf("Could not determine OS version, assuming global namespace not needed.\n");
  150. fUseGlobalNamespace = FALSE;
  151. }
  152. /*
  153. * get events start/ack events
  154. */
  155. if (fUseGlobalNamespace)
  156. {
  157. hstartevent = CreateEvent( DNGetNullDacl(), FALSE, FALSE, "Global\\" DPHELP_EVENT_NAME );
  158. }
  159. else
  160. {
  161. hstartevent = CreateEvent( NULL, FALSE, FALSE, DPHELP_EVENT_NAME );
  162. }
  163. printf( "hstartevent = %08lx\n", hstartevent );
  164. if( hstartevent == NULL )
  165. {
  166. return FALSE;
  167. }
  168. if (fUseGlobalNamespace)
  169. {
  170. hackevent = CreateEvent( DNGetNullDacl(), FALSE, FALSE, "Global\\" DPHELP_ACK_EVENT_NAME );
  171. }
  172. else
  173. {
  174. hackevent = CreateEvent( NULL, FALSE, FALSE, DPHELP_ACK_EVENT_NAME );
  175. }
  176. printf( "hackevent = %08lx\n", hackevent );
  177. if( hackevent == NULL )
  178. {
  179. CloseHandle( hstartevent );
  180. return FALSE;
  181. }
  182. /*
  183. * create shared memory area
  184. */
  185. if (fUseGlobalNamespace)
  186. {
  187. hmem = CreateFileMapping( INVALID_HANDLE_VALUE, DNGetNullDacl(),
  188. PAGE_READWRITE, 0, sizeof( DPHELPDATA ),
  189. "Global\\" DPHELP_SHARED_NAME );
  190. }
  191. else
  192. {
  193. hmem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL,
  194. PAGE_READWRITE, 0, sizeof( DPHELPDATA ),
  195. DPHELP_SHARED_NAME );
  196. }
  197. printf( "hmem = %08lx\n", hmem );
  198. if( hmem == NULL )
  199. {
  200. printf( "Could not create file mapping!\n" );
  201. CloseHandle( hstartevent );
  202. CloseHandle( hackevent );
  203. return FALSE;
  204. }
  205. phd = (LPDPHELPDATA) MapViewOfFile( hmem, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
  206. printf( "phd = %08lx\n", phd );
  207. if( phd == NULL )
  208. {
  209. printf( "Could not create view of file!\n" );
  210. CloseHandle( hmem );
  211. CloseHandle( hstartevent );
  212. CloseHandle( hackevent );
  213. return FALSE;
  214. }
  215. /*
  216. * wait for access to the shared memory
  217. */
  218. if (fUseGlobalNamespace)
  219. {
  220. hmutex = OpenMutex( SYNCHRONIZE, FALSE, "Global\\" DPHELP_MUTEX_NAME );
  221. }
  222. else
  223. {
  224. hmutex = OpenMutex( SYNCHRONIZE, FALSE, DPHELP_MUTEX_NAME );
  225. }
  226. printf( "hmutex = %08lx\n", hmutex );
  227. if( hmutex == NULL )
  228. {
  229. printf( "Could not create mutex!\n" );
  230. UnmapViewOfFile( phd );
  231. CloseHandle( hmem );
  232. CloseHandle( hstartevent );
  233. CloseHandle( hackevent );
  234. return FALSE;
  235. }
  236. WaitForSingleObject( hmutex, INFINITE );
  237. /*
  238. * wake up DPHELP with our request
  239. */
  240. memcpy( phd, req_phd, sizeof( DPHELPDATA ) );
  241. printf( "waking up DPHELP\n" );
  242. if( SetEvent( hstartevent ) )
  243. {
  244. printf( "Waiting for response\n" );
  245. WaitForSingleObject( hackevent, INFINITE );
  246. memcpy( req_phd, phd, sizeof( DPHELPDATA ) );
  247. rc = TRUE;
  248. printf( "got response\n" );
  249. }
  250. else
  251. {
  252. printf("Could not signal event to notify DPHELP!\n" );
  253. rc = FALSE;
  254. }
  255. /*
  256. * done with things
  257. */
  258. ReleaseMutex( hmutex );
  259. CloseHandle( hmutex );
  260. CloseHandle( hstartevent );
  261. CloseHandle( hackevent );
  262. UnmapViewOfFile( phd );
  263. CloseHandle( hmem );
  264. return rc;
  265. } /* sendRequest */
  266. // if the main entry point is called "WinMain" we will be built
  267. // as a windows app
  268. #ifdef NOCONSOLE
  269. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  270. LPSTR lpCmdLine, int nCmdShow)
  271. #else
  272. // if the main entry point is called "main" we will be built
  273. // as a console app
  274. int __cdecl main( int argc, char *argv[] )
  275. #endif
  276. {
  277. OSVERSIONINFOA VersionInfo;
  278. BOOL fUseGlobalNamespace;
  279. HANDLE h;
  280. DPHELPDATA hd;
  281. // Determine if we're running on NT.
  282. memset(&VersionInfo, 0, sizeof(VersionInfo));
  283. VersionInfo.dwOSVersionInfoSize = sizeof(VersionInfo);
  284. if (GetVersionExA(&VersionInfo))
  285. {
  286. if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  287. {
  288. printf("Running on NT version %u.%u.%u, using global namespace.\n",
  289. VersionInfo.dwMajorVersion, VersionInfo.dwMinorVersion, VersionInfo.dwBuildNumber);
  290. fUseGlobalNamespace = TRUE;
  291. }
  292. else
  293. {
  294. printf("Running on 9x version %u.%u.%u, not using global namespace.\n",
  295. VersionInfo.dwMajorVersion, VersionInfo.dwMinorVersion, LOWORD(VersionInfo.dwBuildNumber));
  296. fUseGlobalNamespace = FALSE;
  297. }
  298. }
  299. else
  300. {
  301. printf("Could not determine OS version, assuming global namespace not needed.\n");
  302. fUseGlobalNamespace = FALSE;
  303. }
  304. if (fUseGlobalNamespace)
  305. {
  306. h = OpenEvent( SYNCHRONIZE, FALSE, "Global\\" DPHELP_STARTUP_EVENT_NAME );
  307. }
  308. else
  309. {
  310. h = OpenEvent( SYNCHRONIZE, FALSE, DPHELP_STARTUP_EVENT_NAME );
  311. }
  312. if( h == NULL )
  313. {
  314. printf( "Helper not running\n" );
  315. return 0;
  316. }
  317. printf( "*** SUICIDE ***\n" );
  318. hd.req = DPHELPREQ_SUICIDE;
  319. sendRequest( &hd );
  320. // This should be done after functions that use a Dacl will no longer be
  321. // called (CreateMutex, CreateFile, etc).
  322. if (g_pEveryoneACL)
  323. {
  324. HeapFree(GetProcessHeap(), 0, g_pEveryoneACL);
  325. g_pEveryoneACL = NULL;
  326. g_psa = NULL;
  327. g_fDaclInited = FALSE;
  328. }
  329. return 0;
  330. }