1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
// -=- WinVNC Version 4.0 Service-Mode implementation
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <winvnc/VNCServerService.h>
#include <rfb/LogWriter.h>
#include <rfb_win32/TsSessions.h>
#include <rfb_win32/ModuleFileName.h>
#include <windows.h>
#include <wtsapi32.h>
#include <tlhelp32.h>
using namespace winvnc;
using namespace rfb;
using namespace win32;
const char* winvnc::VNCServerService::Name = "TigerVNC";
// SendSAS is not available until Windows 7, and missing from MinGW
static HMODULE sasLibrary = NULL;
typedef void WINAPI (*SendSAS_proto)(BOOL AsUser);
static SendSAS_proto _SendSAS = NULL;
VNCServerService::VNCServerService()
: Service(Name)
, stopServiceEvent(CreateEvent(0, FALSE, FALSE, 0))
, sessionEvent(CreateEvent(0, FALSE, FALSE, "Global\\SessionEventTigerVNC"))
, sessionEventCad(CreateEvent(0, FALSE, FALSE, "Global\\SessionEventTigerVNCCad")) {
if (sasLibrary == NULL) {
sasLibrary = LoadLibrary("sas.dll");
if (sasLibrary != NULL)
_SendSAS = (SendSAS_proto)(void*)GetProcAddress(sasLibrary, "SendSAS");
}
// - Set the service-mode logging defaults
// These will be overridden by the Log option in the
// registry, if present.
logParams.setParam("*:EventLog:0,Connections:EventLog:100");
}
//////////////////////////////////////////////////////////////////////////////
DWORD GetLogonPid(DWORD dwSessionId)
{
DWORD dwLogonPid = 0;
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof procEntry;
if (Process32First(hSnap, &procEntry)) do
{
DWORD dwLogonSessionId = 0;
if (_stricmp(procEntry.szExeFile, "winlogon.exe") == 0 &&
ProcessIdToSessionId(procEntry.th32ProcessID, &dwLogonSessionId) &&
dwLogonSessionId == dwSessionId)
{
dwLogonPid = procEntry.th32ProcessID;
break;
}
} while (Process32Next(hSnap, &procEntry));
CloseHandle(hSnap);
}
return dwLogonPid;
}
//////////////////////////////////////////////////////////////////////////////
BOOL GetSessionUserTokenWin(OUT LPHANDLE lphUserToken)
{
BOOL bResult = FALSE;
ConsoleSessionId ID_session;
DWORD Id = GetLogonPid(ID_session.id);
if (HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, Id))
{
bResult = OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, lphUserToken);
CloseHandle(hProcess);
}
return bResult;
}
//////////////////////////////////////////////////////////////////////////////
// START the app as system
HANDLE LaunchProcessWin(DWORD /*dwSessionId*/)
{
HANDLE hProcess = NULL;
HANDLE hToken = NULL;
if (GetSessionUserTokenWin(&hToken))
{
ModuleFileName filename;
CharArray cmdLine;
cmdLine.format("\"%s\" -noconsole -service_run", filename.buf);
STARTUPINFO si;
ZeroMemory(&si, sizeof si);
si.cb = sizeof si;
si.dwFlags = STARTF_USESHOWWINDOW;
PROCESS_INFORMATION pi;
if (CreateProcessAsUser(hToken, NULL, cmdLine.buf, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
hProcess = pi.hProcess;
}
CloseHandle(hToken);
}
return hProcess;
}
DWORD VNCServerService::serviceMain(int /*argc*/, char* /*argv*/ [])
{
ConsoleSessionId OlddwSessionId;
HANDLE hProcess = NULL;
//We use this event to notify the program that the session has changed
//The program need to end so the service can restart the program in the correct session
//wait_for_existing_process();
HANDLE testevent[2] = { stopServiceEvent, sessionEventCad };
setStatus(SERVICE_RUNNING);
while (status.dwCurrentState == SERVICE_RUNNING)
{
DWORD dwEvent = WaitForMultipleObjects(2, testevent, FALSE, 1000);
switch (dwEvent)
{
//stopServiceEvent, exit while loop
case WAIT_OBJECT_0 + 0:
setStatus(SERVICE_STOP_PENDING);
break;
//cad request
case WAIT_OBJECT_0 + 1:
if (_SendSAS != NULL)
_SendSAS(FALSE);
break;
case WAIT_TIMEOUT:
{
ConsoleSessionId dwSessionId;
if (OlddwSessionId.id != dwSessionId.id)
{
OlddwSessionId.id = dwSessionId.id;
SetEvent(sessionEvent);
}
DWORD dwExitCode = 0;
if (hProcess == NULL ||
(GetExitCodeProcess(hProcess, &dwExitCode) &&
dwExitCode != STILL_ACTIVE &&
CloseHandle(hProcess)))
{
hProcess = LaunchProcessWin(dwSessionId.id);
}
}
break;
}
}
SetEvent(sessionEvent);
if (hProcess)
{
WaitForSingleObject(hProcess, 15000);
CloseHandle(hProcess);
}
return 0;
}
void VNCServerService::stop() {
SetEvent(stopServiceEvent);
SetEvent(sessionEvent);
}
|