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.

323 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. coninit.c
  5. Abstract:
  6. This module contains the code to initialize the Console Port of the POSIX
  7. Emulation Subsystem.
  8. Author:
  9. Avi Nathan (avin) 17-Jul-1991
  10. Environment:
  11. User Mode Only
  12. Revision History:
  13. --*/
  14. #include "psxsrv.h"
  15. #include <windows.h>
  16. #define NTPSX_ONLY
  17. #include "sesport.h"
  18. NTSTATUS
  19. PsxInitializeConsolePort(
  20. VOID
  21. )
  22. {
  23. NTSTATUS Status;
  24. UNICODE_STRING PsxSessionDirectoryName_U;
  25. UNICODE_STRING PsxSessionPortName_U;
  26. OBJECT_ATTRIBUTES ObjectAttributes;
  27. CHAR cchSecurityDescriptor [SECURITY_DESCRIPTOR_MIN_LENGTH];
  28. PSECURITY_DESCRIPTOR pSecurityDescriptor = (PSECURITY_DESCRIPTOR) cchSecurityDescriptor;
  29. BOOLEAN bAllocDirectoryName = FALSE;
  30. /*
  31. ** Create a directory in the object name space for the session port
  32. ** names
  33. */
  34. PSX_GET_SESSION_OBJECT_NAME(&PsxSessionPortName_U,PSX_SS_SESSION_PORT_NAME);
  35. PSX_GET_CREATE_UNICODE_STRING_FROM_ASCIIZ(&PsxSessionDirectoryName_U,PSX_SES_BASE_PORT_NAME,bAllocDirectoryName);
  36. Status = (bAllocDirectoryName) ? STATUS_SUCCESS : STATUS_NO_MEMORY;
  37. if (NT_SUCCESS (Status)) {
  38. Status = PsxCreateDirectoryObject (&PsxSessionDirectoryName_U);
  39. }
  40. IF_PSX_DEBUG(LPC) {
  41. KdPrint(("PSXSS: Creating %wZ port and associated thread\n", &PsxSessionPortName_U ));
  42. }
  43. if (NT_SUCCESS (Status)) {
  44. Status = RtlCreateSecurityDescriptor (pSecurityDescriptor,
  45. SECURITY_DESCRIPTOR_REVISION);
  46. }
  47. if (NT_SUCCESS (Status)) {
  48. Status = RtlSetDaclSecurityDescriptor (pSecurityDescriptor, TRUE, NULL, FALSE);
  49. }
  50. if (NT_SUCCESS (Status)) {
  51. InitializeObjectAttributes (&ObjectAttributes,
  52. &PsxSessionPortName_U,
  53. 0,
  54. NULL,
  55. pSecurityDescriptor);
  56. Status = NtCreatePort (&PsxSessionPort,
  57. &ObjectAttributes,
  58. sizeof (PSXSESCONNECTINFO),
  59. sizeof (PSXSESREQUESTMSG),
  60. sizeof (PSXSESREQUESTMSG) * 32);
  61. }
  62. #if BOGUS_THREADS
  63. ASSERT(NT_SUCCESS(Status));
  64. Status = RtlCreateUserThread (NtCurrentProcess(),
  65. NULL,
  66. TRUE,
  67. 0,
  68. 0,
  69. 0,
  70. PsxSessionRequestThread,
  71. NULL,
  72. &PsxSessionRequestThreadHandle,
  73. NULL);
  74. ASSERT(NT_SUCCESS(Status));
  75. #else
  76. if (NT_SUCCESS (Status)) {
  77. DWORD Id;
  78. PsxSessionRequestThreadHandle = CreateThread (NULL,
  79. 0,
  80. (LPTHREAD_START_ROUTINE)PsxSessionRequestThread,
  81. NULL,
  82. CREATE_SUSPENDED,
  83. &Id);
  84. }
  85. #endif
  86. /*
  87. ** BUGBUG: this guy is going to spin for quite a while until
  88. ** he does something
  89. */
  90. if (NT_SUCCESS (Status)) {
  91. Status = NtResumeThread (PsxSessionRequestThreadHandle, NULL);
  92. }
  93. if (bAllocDirectoryName) RtlFreeUnicodeString (&PsxSessionDirectoryName_U);
  94. return Status;
  95. }
  96. NTSTATUS
  97. PsxCreateDirectoryObject(
  98. PUNICODE_STRING pUnicodeDirectoryName
  99. )
  100. /*++
  101. Routine Description
  102. This function is called to create a directory object of the
  103. specified name. It ensures that the object has the appropriate
  104. permissions, protections etc.
  105. Arguments:
  106. pUnicodeDirectoryName - the full path name of the directory
  107. to be created in a unicode format.
  108. Return Value:
  109. Status of operation.
  110. --*/
  111. {
  112. NTSTATUS Status;
  113. HANDLE DirectoryHandle;
  114. OBJECT_ATTRIBUTES ObjectAttributes;
  115. CHAR cchSecurityDescriptor [SECURITY_DESCRIPTOR_MIN_LENGTH];
  116. PSECURITY_DESCRIPTOR pSecurityDescriptor = (PSECURITY_DESCRIPTOR) cchSecurityDescriptor;
  117. PSID
  118. pSidAdmin,
  119. pSidSystem,
  120. pSidWorld;
  121. SID_IDENTIFIER_AUTHORITY
  122. AuthorityNt = SECURITY_NT_AUTHORITY,
  123. AuthorityWorld = SECURITY_WORLD_SID_AUTHORITY;
  124. ACCESS_MASK
  125. AccessMask = (DIRECTORY_ALL_ACCESS) & ~(WRITE_DAC | WRITE_OWNER | DELETE);
  126. ULONG
  127. cbDaclLength;
  128. PACL
  129. pDacl;
  130. PACE_HEADER
  131. Ace;
  132. BOOLEAN
  133. bAllocSidAdmin = FALSE,
  134. bAllocSidSystem = FALSE,
  135. bAllocSidWorld = FALSE,
  136. bAllocDacl = FALSE;
  137. Status = RtlCreateSecurityDescriptor (pSecurityDescriptor,
  138. SECURITY_DESCRIPTOR_REVISION);
  139. if (NT_SUCCESS (Status)) {
  140. Status = RtlAllocateAndInitializeSid (&AuthorityNt,
  141. 2,
  142. SECURITY_BUILTIN_DOMAIN_RID,
  143. DOMAIN_ALIAS_RID_ADMINS,
  144. 0, 0, 0, 0, 0, 0,
  145. &pSidAdmin);
  146. bAllocSidAdmin = NT_SUCCESS (Status);
  147. }
  148. if (NT_SUCCESS (Status)) {
  149. Status = RtlAllocateAndInitializeSid (&AuthorityNt,
  150. 1,
  151. SECURITY_LOCAL_SYSTEM_RID,
  152. 0,
  153. 0, 0, 0, 0, 0, 0,
  154. &pSidSystem);
  155. bAllocSidSystem = NT_SUCCESS (Status);
  156. }
  157. if (NT_SUCCESS (Status)) {
  158. Status = RtlAllocateAndInitializeSid (&AuthorityWorld,
  159. 1,
  160. SECURITY_WORLD_RID,
  161. 0,
  162. 0, 0, 0, 0, 0, 0,
  163. &pSidWorld);
  164. bAllocSidWorld = NT_SUCCESS (Status);
  165. }
  166. if (NT_SUCCESS (Status)) {
  167. cbDaclLength = sizeof (ACL)
  168. + 3 * sizeof (ACCESS_ALLOWED_ACE)
  169. + RtlLengthSid (pSidAdmin)
  170. + RtlLengthSid (pSidSystem)
  171. + RtlLengthSid (pSidWorld);
  172. pDacl = RtlAllocateHeap (RtlProcessHeap(), 0, cbDaclLength);
  173. if (NULL == pDacl) {
  174. Status = STATUS_NO_MEMORY;
  175. }
  176. else {
  177. bAllocDacl = TRUE;
  178. }
  179. }
  180. /*
  181. ** Create the Dacl and then add the ACEs
  182. */
  183. if (NT_SUCCESS(Status)) {
  184. Status = RtlCreateAcl (pDacl, cbDaclLength, ACL_REVISION);
  185. }
  186. if (NT_SUCCESS(Status)) {
  187. Status = RtlAddAccessAllowedAce (pDacl, ACL_REVISION, GENERIC_ALL, pSidAdmin);
  188. }
  189. if (NT_SUCCESS(Status)) {
  190. Status = RtlAddAccessAllowedAce (pDacl, ACL_REVISION, GENERIC_ALL, pSidSystem);
  191. }
  192. if (NT_SUCCESS(Status)) {
  193. Status = RtlAddAccessAllowedAce (pDacl, ACL_REVISION, AccessMask, pSidWorld);
  194. }
  195. /*
  196. ** Put the Dacl in the security descriptor
  197. */
  198. if (NT_SUCCESS(Status)) {
  199. Status = RtlSetDaclSecurityDescriptor (pSecurityDescriptor, TRUE, pDacl, FALSE);
  200. }
  201. if (NT_SUCCESS (Status)) {
  202. if (NtCurrentPeb()->SessionId) {
  203. InitializeObjectAttributes (&ObjectAttributes,
  204. pUnicodeDirectoryName,
  205. 0,
  206. NULL,
  207. pSecurityDescriptor);
  208. }else{
  209. InitializeObjectAttributes (&ObjectAttributes,
  210. pUnicodeDirectoryName,
  211. OBJ_PERMANENT,
  212. NULL,
  213. pSecurityDescriptor);
  214. }
  215. Status = NtCreateDirectoryObject (&DirectoryHandle,
  216. DIRECTORY_ALL_ACCESS,
  217. &ObjectAttributes);
  218. }
  219. if (bAllocDacl) RtlFreeHeap (RtlProcessHeap(), 0, pDacl);
  220. if (bAllocSidWorld) RtlFreeHeap (RtlProcessHeap(), 0, pSidWorld);
  221. if (bAllocSidSystem) RtlFreeHeap (RtlProcessHeap(), 0, pSidSystem);
  222. if (bAllocSidAdmin) RtlFreeHeap (RtlProcessHeap(), 0, pSidAdmin);
  223. return Status;
  224. }