00028 {
00029
NTSTATUS Status;
00030 HANDLE hToken;
00031 ULONG ulLength;
00032 PTOKEN_USER ptuService;
00033 PSECURITY_DESCRIPTOR psdService;
00034 PSID psid;
00035 PACCESS_ALLOWED_ACE paceService =
NULL, pace;
00036 OBJECT_ATTRIBUTES ObjService;
00037 HWINSTA hwinsta;
00038 UNICODE_STRING strDesktop;
00039
TL tlPoolSdService, tlPoolAceService, tlPoolToken;
00040
00041
00042
00043
00044
Status =
OpenEffectiveToken(&hToken);
00045
if (!
NT_SUCCESS(Status)) {
00046 RIPMSG1(RIP_WARNING,
"ConnectService: Could not open process/thread token (0x%X)", Status);
00047
return NULL;
00048 }
00049
00050
00051
00052
00053 ptuService =
NULL;
00054 paceService =
NULL;
00055 psdService =
NULL;
00056 hwinsta =
NULL;
00057 ZwQueryInformationToken(hToken, TokenUser, NULL, 0, &ulLength);
00058 ptuService = (PTOKEN_USER)UserAllocPool(ulLength, TAG_TOKEN);
00059
if (ptuService ==
NULL) {
00060 RIPMSG1(RIP_WARNING,
"ConnectService: Can't alloc buffer (size=%d) for token info", ulLength);
00061
goto sd_error;
00062 }
00063
Status = ZwQueryInformationToken(hToken, TokenUser, ptuService,
00064 ulLength, &ulLength);
00065 ZwClose(hToken);
00066
if (!
NT_SUCCESS(Status)) {
00067 RIPMSG1(RIP_WARNING,
"ConnectService: QueryInformationToken failed (0x%X)", Status);
00068
goto sd_error;
00069 }
00070 psid = ptuService->User.Sid;
00071
00072
00073
00074
00075 paceService =
AllocAce(NULL, ACCESS_ALLOWED_ACE_TYPE, 0,
00076 WINSTA_CREATEDESKTOP | WINSTA_READATTRIBUTES |
00077 WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS |
00078 WINSTA_ACCESSCLIPBOARD | STANDARD_RIGHTS_REQUIRED,
00079 psid, &ulLength);
00080
if (paceService ==
NULL) {
00081 RIPMSG0(RIP_WARNING,
"ConnectService: AllocAce for WindowStation attributes failed");
00082
goto sd_error;
00083 }
00084 pace =
AllocAce(paceService, ACCESS_ALLOWED_ACE_TYPE, OBJECT_INHERIT_ACE |
00085 INHERIT_ONLY_ACE | NO_PROPAGATE_INHERIT_ACE,
00086 DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS | DESKTOP_ENUMERATE |
00087 DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL |
00088 STANDARD_RIGHTS_REQUIRED,
00089 psid, &ulLength);
00090
if (pace ==
NULL) {
00091 RIPMSG0(RIP_WARNING,
"ConnectService: AllocAce for Desktop Attributes failed");
00092
goto sd_error;
00093 }
00094 paceService = pace;
00095 pace =
AllocAce(pace, ACCESS_ALLOWED_ACE_TYPE, 0,
00096 WINSTA_ENUMERATE,
00097
SeExports->
SeAliasAdminsSid, &ulLength);
00098
if (pace ==
NULL) {
00099 RIPMSG0(RIP_WARNING,
"ConnectService: AllocAce for admin WinSta enumerate failed");
00100
goto sd_error;
00101 }
00102 paceService = pace;
00103 pace =
AllocAce(pace, ACCESS_ALLOWED_ACE_TYPE, OBJECT_INHERIT_ACE |
00104 INHERIT_ONLY_ACE | NO_PROPAGATE_INHERIT_ACE,
00105 DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS | DESKTOP_ENUMERATE,
00106
SeExports->
SeAliasAdminsSid, &ulLength);
00107
if (pace ==
NULL) {
00108 RIPMSG0(RIP_WARNING,
"ConnectService: AllocAce for admin Desktop access failed");
00109
goto sd_error;
00110 }
00111 paceService = pace;
00112
00113
00114
00115
00116 psdService =
CreateSecurityDescriptor(paceService, ulLength, FALSE);
00117
if (psdService ==
NULL) {
00118 RIPMSG0(RIP_WARNING,
"ConnectService: CreateSecurityDescriptor failed");
00119
goto sd_error;
00120 }
00121
00122
ThreadLockPool(
PtiCurrent(), ptuService, &tlPoolToken);
00123
ThreadLockPool(
PtiCurrent(), paceService, &tlPoolAceService);
00124
ThreadLockPool(
PtiCurrent(), psdService, &tlPoolSdService);
00125
00126
00127
00128
00129 InitializeObjectAttributes(&ObjService, pstrWinSta,
00130 OBJ_OPENIF, NULL, psdService);
00131 hwinsta =
xxxCreateWindowStation(&ObjService,
00132 KernelMode,
00133 MAXIMUM_ALLOWED,
00134 NULL, 0, NULL, 0);
00135
if (hwinsta !=
NULL) {
00136
00137
TRACE_INIT((
"Service windowstation created\n"));
00138
00139
00140
00141
00142
00143
00144
00145
RtlInitUnicodeString(&strDesktop, TEXT(
"Default"));
00146 InitializeObjectAttributes(&ObjService, &strDesktop,
00147 OBJ_OPENIF | OBJ_CASE_INSENSITIVE, hwinsta, NULL);
00148
00149 *phdesk =
xxxCreateDesktop(&ObjService, KernelMode,
00150 NULL, NULL, 0, MAXIMUM_ALLOWED);
00151
00152
if (*phdesk ==
NULL) {
00153
00154
00155
00156
00157
00158 RIPMSG0(RIP_WARNING,
"ConnectService: CreateDesktop('Default') failed.");
00159
00160 ZwClose(hwinsta);
00161 hwinsta =
NULL;
00162 }
else {
00163
TRACE_INIT((
"Default desktop in Service windowstation created\n"));
00164 }
00165 }
else {
00166 *phdesk =
NULL;
00167 }
00168
00169
ThreadUnlockPool(
PtiCurrent(), &tlPoolSdService);
00170
ThreadUnlockPool(
PtiCurrent(), &tlPoolAceService);
00171
ThreadUnlockPool(
PtiCurrent(), &tlPoolToken);
00172
00173 sd_error:
00174
if (ptuService !=
NULL)
00175 UserFreePool(ptuService);
00176
if (paceService !=
NULL)
00177 UserFreePool(paceService);
00178
if (psdService !=
NULL)
00179 UserFreePool(psdService);
00180
00181
return hwinsta;
00182 }