CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/vncdll/winvnc/TsSessions.cpp
Views: 11780
1
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2
* Copyright (C) 2007 Constantin Kaplinsky. All Rights Reserved.
3
*
4
* This is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
8
*
9
* This software is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this software; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
* USA.
18
*/
19
20
#include "TsSessions.h"
21
#include "DynamicFn.h"
22
#include <tchar.h>
23
24
#ifdef ERROR_CTX_WINSTATION_BUSY
25
#define RFB_HAVE_WINSTATION_CONNECT
26
#else
27
#pragma message(" NOTE: Not building WinStationConnect support.")
28
#endif
29
30
// Windows XP (and later) functions used to handle session Ids
31
typedef BOOLEAN (WINAPI *_WinStationConnect_proto) (HANDLE,ULONG,ULONG,PCWSTR,ULONG);
32
DynamicFn<_WinStationConnect_proto> _WinStationConnect(_T("winsta.dll"), "WinStationConnectW");
33
typedef DWORD (WINAPI *_WTSGetActiveConsoleSessionId_proto) ();
34
DynamicFn<_WTSGetActiveConsoleSessionId_proto> _WTSGetActiveConsoleSessionId(_T("kernel32.dll"), "WTSGetActiveConsoleSessionId");
35
typedef BOOL (WINAPI *_ProcessIdToSessionId_proto) (DWORD, DWORD*);
36
DynamicFn<_ProcessIdToSessionId_proto> _ProcessIdToSessionId(_T("kernel32.dll"), "ProcessIdToSessionId");
37
typedef BOOL (WINAPI *_LockWorkStation_proto)();
38
DynamicFn<_LockWorkStation_proto> _LockWorkStation(_T("user32.dll"), "LockWorkStation");
39
40
41
ProcessSessionId::ProcessSessionId(DWORD processId) {
42
id = 0;
43
if (!_ProcessIdToSessionId.isValid())
44
return;
45
if (processId == -1)
46
processId = GetCurrentProcessId();
47
(*_ProcessIdToSessionId)(GetCurrentProcessId(), &id);
48
}
49
50
ProcessSessionId mySessionId;
51
52
ConsoleSessionId::ConsoleSessionId() {
53
if (_WTSGetActiveConsoleSessionId.isValid())
54
id = (*_WTSGetActiveConsoleSessionId)();
55
else
56
id = 0;
57
}
58
59
bool inConsoleSession() {
60
ConsoleSessionId console;
61
return console.id == mySessionId.id;
62
}
63
64
void setConsoleSession(DWORD sessionId) {
65
#ifdef RFB_HAVE_WINSTATION_CONNECT
66
if (!_WinStationConnect.isValid()) {
67
return;
68
}
69
if (sessionId == -1)
70
sessionId = mySessionId.id;
71
72
// Try to reconnect our session to the console
73
ConsoleSessionId console;
74
if (!(*_WinStationConnect)(0, sessionId, console.id, L"", 0)) {
75
return;
76
}
77
78
// Lock the newly connected session, for security
79
if (_LockWorkStation.isValid())
80
(*_LockWorkStation)();
81
#else
82
83
#endif
84
}
85
86