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.

203 lines
6.9 KiB

  1. /**************************** Module Header ********************************\
  2. * Module Name: service.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * Service Support Routines
  7. *
  8. * History:
  9. * 12-22-93 JimA Created.
  10. \***************************************************************************/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. /***************************************************************************\
  14. * xxxConnectService
  15. *
  16. * Open the windowstation assigned to the service logon session. If no
  17. * windowstation exists, create the windowstation and a default desktop.
  18. *
  19. * History:
  20. * 12-23-93 JimA Created.
  21. \***************************************************************************/
  22. HWINSTA xxxConnectService(
  23. PUNICODE_STRING pstrWinSta,
  24. HDESK *phdesk)
  25. {
  26. NTSTATUS Status;
  27. HANDLE hToken;
  28. ULONG ulLength;
  29. PTOKEN_USER ptuService;
  30. PSECURITY_DESCRIPTOR psdService;
  31. PSID psid;
  32. PACCESS_ALLOWED_ACE paceService = NULL, pace;
  33. OBJECT_ATTRIBUTES ObjService;
  34. HWINSTA hwinsta;
  35. UNICODE_STRING strDesktop;
  36. TL tlPoolSdService, tlPoolAceService, tlPoolToken;
  37. /*
  38. * Open the token of the service.
  39. */
  40. Status = OpenEffectiveToken(&hToken);
  41. if (!NT_SUCCESS(Status)) {
  42. RIPMSG1(RIP_WARNING, "ConnectService: Could not open process/thread token (0x%X)", Status);
  43. return NULL;
  44. }
  45. /*
  46. * Get the user SID assigned to the service.
  47. */
  48. ptuService = NULL;
  49. paceService = NULL;
  50. psdService = NULL;
  51. hwinsta = NULL;
  52. ZwQueryInformationToken(hToken, TokenUser, NULL, 0, &ulLength);
  53. ptuService = (PTOKEN_USER)UserAllocPool(ulLength, TAG_TOKEN);
  54. if (ptuService == NULL) {
  55. RIPMSG1(RIP_WARNING, "ConnectService: Can't alloc buffer (size=%d) for token info", ulLength);
  56. ZwClose(hToken);
  57. goto sd_error;
  58. }
  59. Status = ZwQueryInformationToken(hToken, TokenUser, ptuService,
  60. ulLength, &ulLength);
  61. ZwClose(hToken);
  62. if (!NT_SUCCESS(Status)) {
  63. RIPMSG1(RIP_WARNING, "ConnectService: QueryInformationToken failed (0x%X)", Status);
  64. goto sd_error;
  65. }
  66. psid = ptuService->User.Sid;
  67. /*
  68. * Create ACE list.
  69. */
  70. paceService = AllocAce(NULL, ACCESS_ALLOWED_ACE_TYPE, 0,
  71. WINSTA_CREATEDESKTOP | WINSTA_READATTRIBUTES |
  72. WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS |
  73. WINSTA_ACCESSCLIPBOARD | STANDARD_RIGHTS_REQUIRED,
  74. psid, &ulLength);
  75. if (paceService == NULL) {
  76. RIPMSG0(RIP_WARNING, "ConnectService: AllocAce for WindowStation attributes failed");
  77. goto sd_error;
  78. }
  79. pace = AllocAce(paceService, ACCESS_ALLOWED_ACE_TYPE, OBJECT_INHERIT_ACE |
  80. INHERIT_ONLY_ACE | NO_PROPAGATE_INHERIT_ACE,
  81. DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS | DESKTOP_ENUMERATE |
  82. DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL |
  83. STANDARD_RIGHTS_REQUIRED,
  84. psid, &ulLength);
  85. if (pace == NULL) {
  86. RIPMSG0(RIP_WARNING, "ConnectService: AllocAce for Desktop Attributes failed");
  87. goto sd_error;
  88. }
  89. paceService = pace;
  90. pace = AllocAce(pace, ACCESS_ALLOWED_ACE_TYPE, 0,
  91. WINSTA_ENUMERATE,
  92. SeExports->SeAliasAdminsSid, &ulLength);
  93. if (pace == NULL) {
  94. RIPMSG0(RIP_WARNING, "ConnectService: AllocAce for admin WinSta enumerate failed");
  95. goto sd_error;
  96. }
  97. paceService = pace;
  98. pace = AllocAce(pace, ACCESS_ALLOWED_ACE_TYPE, OBJECT_INHERIT_ACE |
  99. INHERIT_ONLY_ACE | NO_PROPAGATE_INHERIT_ACE,
  100. DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS | DESKTOP_ENUMERATE,
  101. SeExports->SeAliasAdminsSid, &ulLength);
  102. if (pace == NULL) {
  103. RIPMSG0(RIP_WARNING, "ConnectService: AllocAce for admin Desktop access failed");
  104. goto sd_error;
  105. }
  106. paceService = pace;
  107. /*
  108. * Initialize the SD
  109. */
  110. psdService = CreateSecurityDescriptor(paceService, ulLength, FALSE);
  111. if (psdService == NULL) {
  112. RIPMSG0(RIP_WARNING, "ConnectService: CreateSecurityDescriptor failed");
  113. goto sd_error;
  114. }
  115. ThreadLockPool(PtiCurrent(), ptuService, &tlPoolToken);
  116. ThreadLockPool(PtiCurrent(), paceService, &tlPoolAceService);
  117. ThreadLockPool(PtiCurrent(), psdService, &tlPoolSdService);
  118. /*
  119. * The windowstation does not exist and must be created.
  120. */
  121. try {
  122. InitializeObjectAttributes(&ObjService,
  123. pstrWinSta,
  124. OBJ_OPENIF,
  125. NULL,
  126. psdService);
  127. } except (W32ExceptionHandler(FALSE, RIP_WARNING)) {
  128. Status = GetExceptionCode();
  129. }
  130. if (NT_SUCCESS(Status)) {
  131. hwinsta = xxxCreateWindowStation(&ObjService,
  132. KernelMode,
  133. MAXIMUM_ALLOWED,
  134. NULL,
  135. 0,
  136. NULL,
  137. NULL,
  138. 0);
  139. }
  140. if (hwinsta != NULL) {
  141. TRACE_INIT(("Service windowstation created\n"));
  142. /*
  143. * We have the windowstation, now create the desktop. The security
  144. * descriptor will be inherited from the windowstation. Save the
  145. * winsta handle because the access struct may be moved by the
  146. * desktop creation.
  147. */
  148. RtlInitUnicodeString(&strDesktop, TEXT("Default"));
  149. InitializeObjectAttributes(&ObjService, &strDesktop,
  150. OBJ_OPENIF | OBJ_CASE_INSENSITIVE, hwinsta, NULL);
  151. *phdesk = xxxCreateDesktop(&ObjService,
  152. KernelMode,
  153. NULL,
  154. NULL,
  155. 0,
  156. MAXIMUM_ALLOWED);
  157. if (*phdesk == NULL) {
  158. /*
  159. * The creation failed so close the windowstation and leave.
  160. */
  161. RIPMSGF0(RIP_WARNING, "CreateDesktop('Default') failed.");
  162. ZwClose(hwinsta);
  163. hwinsta = NULL;
  164. } else {
  165. TRACE_INIT(("Default desktop in Service windowstation created\n"));
  166. }
  167. } else {
  168. *phdesk = NULL;
  169. }
  170. ThreadUnlockPool(PtiCurrent(), &tlPoolSdService);
  171. ThreadUnlockPool(PtiCurrent(), &tlPoolAceService);
  172. ThreadUnlockPool(PtiCurrent(), &tlPoolToken);
  173. sd_error:
  174. if (ptuService != NULL) {
  175. UserFreePool(ptuService);
  176. }
  177. if (paceService != NULL) {
  178. UserFreePool(paceService);
  179. }
  180. if (psdService != NULL) {
  181. UserFreePool(psdService);
  182. }
  183. return hwinsta;
  184. }