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.

204 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. apiinit.c
  5. Abstract:
  6. This module contains the code to initialize the ApiPort of the
  7. Server side of the Client-Server Runtime Subsystem to the Session
  8. Manager SubSystem.
  9. Author:
  10. Steve Wood (stevewo) 8-Oct-1990
  11. Environment:
  12. User Mode Only
  13. Revision History:
  14. --*/
  15. #include "csrsrv.h"
  16. static SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  17. static SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  18. NTSTATUS
  19. CsrApiPortInitialize( VOID )
  20. {
  21. NTSTATUS Status;
  22. OBJECT_ATTRIBUTES ObjectAttributes;
  23. HANDLE Thread;
  24. CLIENT_ID ClientId;
  25. PLIST_ENTRY ListHead, ListNext;
  26. PCSR_THREAD ServerThread;
  27. HANDLE EventHandle;
  28. ULONG Length;
  29. PSID SeWorldSid;
  30. PSID SeRestrictedSid;
  31. PSECURITY_DESCRIPTOR SecurityDescriptor;
  32. PACL Dacl;
  33. // Though this function does not seem to cleanup on failure, failure
  34. // will cause Csrss to exit, so any allocated memory will be freed and
  35. // any open handle will be closed.
  36. Length = CsrDirectoryName.Length +
  37. sizeof( CSR_API_PORT_NAME ) +
  38. sizeof( OBJ_NAME_PATH_SEPARATOR );
  39. CsrApiPortName.Buffer = RtlAllocateHeap( CsrHeap, MAKE_TAG( INIT_TAG ), Length );
  40. if (CsrApiPortName.Buffer == NULL) {
  41. return( STATUS_NO_MEMORY );
  42. }
  43. CsrApiPortName.Length = 0;
  44. CsrApiPortName.MaximumLength = (USHORT)Length;
  45. RtlAppendUnicodeStringToString( &CsrApiPortName, &CsrDirectoryName );
  46. RtlAppendUnicodeToString( &CsrApiPortName, L"\\" );
  47. RtlAppendUnicodeToString( &CsrApiPortName, CSR_API_PORT_NAME );
  48. IF_CSR_DEBUG( INIT ) {
  49. DbgPrint( "CSRSS: Creating %wZ port and associated threads\n",
  50. &CsrApiPortName );
  51. DbgPrint( "CSRSS: sizeof( CONNECTINFO ) == %ld sizeof( API_MSG ) == %ld\n",
  52. sizeof( CSR_API_CONNECTINFO ),
  53. sizeof( CSR_API_MSG )
  54. );
  55. }
  56. //
  57. // create a security descriptor that allows all access
  58. //
  59. SeWorldSid = RtlAllocateHeap( CsrHeap, MAKE_TAG( TMP_TAG ), RtlLengthRequiredSid( 1 ) );
  60. if (SeWorldSid == NULL) {
  61. return( STATUS_NO_MEMORY );
  62. }
  63. RtlInitializeSid( SeWorldSid, &WorldSidAuthority, 1 );
  64. *(RtlSubAuthoritySid( SeWorldSid, 0 )) = SECURITY_WORLD_RID;
  65. Status = RtlAllocateAndInitializeSid(&NtAuthority ,
  66. 1,
  67. SECURITY_RESTRICTED_CODE_RID,
  68. 0, 0, 0, 0, 0, 0, 0,
  69. &SeRestrictedSid);
  70. if (!NT_SUCCESS(Status)){
  71. return Status;
  72. }
  73. Length = SECURITY_DESCRIPTOR_MIN_LENGTH +
  74. (ULONG)sizeof(ACL) +
  75. 2 * (ULONG)sizeof(ACCESS_ALLOWED_ACE) +
  76. RtlLengthSid( SeWorldSid ) +
  77. RtlLengthSid( SeRestrictedSid ) +
  78. 8; // The 8 is just for good measure
  79. SecurityDescriptor = RtlAllocateHeap( CsrHeap, MAKE_TAG( TMP_TAG ), Length);
  80. if (SecurityDescriptor == NULL) {
  81. return( STATUS_NO_MEMORY );
  82. }
  83. Dacl = (PACL)((PCHAR)SecurityDescriptor + SECURITY_DESCRIPTOR_MIN_LENGTH);
  84. RtlCreateSecurityDescriptor(SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  85. RtlCreateAcl( Dacl, Length - SECURITY_DESCRIPTOR_MIN_LENGTH, ACL_REVISION2);
  86. RtlAddAccessAllowedAce (
  87. Dacl,
  88. ACL_REVISION2,
  89. PORT_ALL_ACCESS,
  90. SeWorldSid
  91. );
  92. RtlAddAccessAllowedAce (
  93. Dacl,
  94. ACL_REVISION2,
  95. PORT_ALL_ACCESS,
  96. SeRestrictedSid
  97. );
  98. RtlSetDaclSecurityDescriptor (
  99. SecurityDescriptor,
  100. TRUE,
  101. Dacl,
  102. FALSE
  103. );
  104. InitializeObjectAttributes( &ObjectAttributes, &CsrApiPortName, 0,
  105. NULL, SecurityDescriptor );
  106. Status = NtCreatePort( &CsrApiPort,
  107. &ObjectAttributes,
  108. sizeof( CSR_API_CONNECTINFO ),
  109. sizeof( CSR_API_MSG ),
  110. 4096 * 16
  111. );
  112. if (!NT_SUCCESS(Status)){
  113. return Status;
  114. }
  115. //
  116. // clean up security stuff
  117. //
  118. RtlFreeHeap( CsrHeap, 0, SeWorldSid );
  119. RtlFreeHeap( CsrHeap, 0, SeRestrictedSid );
  120. RtlFreeHeap( CsrHeap, 0, SecurityDescriptor );
  121. Status = NtCreateEvent(&EventHandle,
  122. EVENT_ALL_ACCESS,
  123. NULL,
  124. SynchronizationEvent,
  125. FALSE
  126. );
  127. if (!NT_SUCCESS(Status)){
  128. return Status;
  129. }
  130. //
  131. // Create the inital request thread
  132. //
  133. Status = RtlCreateUserThread( NtCurrentProcess(),
  134. NULL,
  135. TRUE,
  136. 0,
  137. 0,
  138. 0,
  139. CsrApiRequestThread,
  140. (PVOID)EventHandle,
  141. &Thread,
  142. &ClientId
  143. );
  144. if (!NT_SUCCESS(Status)){
  145. return Status;
  146. }
  147. CsrAddStaticServerThread(Thread,&ClientId,CSR_STATIC_API_THREAD);
  148. ListHead = &CsrRootProcess->ThreadList;
  149. ListNext = ListHead->Flink;
  150. while (ListNext != ListHead) {
  151. ServerThread = CONTAINING_RECORD( ListNext, CSR_THREAD, Link );
  152. Status = NtResumeThread( ServerThread->ThreadHandle, NULL );
  153. if (ServerThread->Flags & CSR_STATIC_API_THREAD) {
  154. Status = NtWaitForSingleObject(EventHandle,FALSE,NULL);
  155. ASSERT( NT_SUCCESS( Status ) );
  156. }
  157. ListNext = ListNext->Flink;
  158. }
  159. NtClose(EventHandle);
  160. return( Status );
  161. }
  162. HANDLE
  163. CsrQueryApiPort(VOID)
  164. {
  165. return CsrApiPort;
  166. }