1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * Server Channels
4  *
5  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  * Copyright 2015 Copyright 2015 Thincast Technologies GmbH
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef FREERDP_LIB_CORE_SERVER_H
22 #define FREERDP_LIB_CORE_SERVER_H
23 
24 #include <freerdp/freerdp.h>
25 #include <freerdp/api.h>
26 #include <freerdp/channels/wtsvc.h>
27 
28 #include <winpr/synch.h>
29 #include <winpr/stream.h>
30 #include <winpr/collections.h>
31 
32 typedef struct rdp_peer_channel rdpPeerChannel;
33 typedef struct WTSVirtualChannelManager WTSVirtualChannelManager;
34 
35 #include "rdp.h"
36 #include "mcs.h"
37 
38 #define CREATE_REQUEST_PDU 0x01
39 #define DATA_FIRST_PDU 0x02
40 #define DATA_PDU 0x03
41 #define CLOSE_REQUEST_PDU 0x04
42 #define CAPABILITY_REQUEST_PDU 0x05
43 
44 enum
45 {
46 	RDP_PEER_CHANNEL_TYPE_SVC = 0,
47 	RDP_PEER_CHANNEL_TYPE_DVC = 1
48 };
49 
50 enum
51 {
52 	DVC_OPEN_STATE_NONE = 0,
53 	DVC_OPEN_STATE_SUCCEEDED = 1,
54 	DVC_OPEN_STATE_FAILED = 2,
55 	DVC_OPEN_STATE_CLOSED = 3
56 };
57 
58 struct rdp_peer_channel
59 {
60 	WTSVirtualChannelManager* vcm;
61 	freerdp_peer* client;
62 
63 	void* extra;
64 	UINT16 index;
65 	UINT32 channelId;
66 	UINT16 channelType;
67 	UINT32 channelFlags;
68 
69 	wStream* receiveData;
70 	wMessageQueue* queue;
71 
72 	BYTE dvc_open_state;
73 	UINT32 dvc_total_length;
74 	rdpMcsChannel* mcsChannel;
75 };
76 
77 struct WTSVirtualChannelManager
78 {
79 	rdpRdp* rdp;
80 	freerdp_peer* client;
81 
82 	DWORD SessionId;
83 	wMessageQueue* queue;
84 
85 	rdpPeerChannel* drdynvc_channel;
86 	BYTE drdynvc_state;
87 	LONG dvc_channel_id_seq;
88 
89 	wArrayList* dynamicVirtualChannels;
90 };
91 
92 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionW(LPWSTR pTargetServerName,
93                                                                 ULONG TargetLogonId, BYTE HotkeyVk,
94                                                                 USHORT HotkeyModifiers);
95 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionA(LPSTR pTargetServerName,
96                                                                 ULONG TargetLogonId, BYTE HotkeyVk,
97                                                                 USHORT HotkeyModifiers);
98 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExW(LPWSTR pTargetServerName,
99                                                                   ULONG TargetLogonId,
100                                                                   BYTE HotkeyVk,
101                                                                   USHORT HotkeyModifiers,
102                                                                   DWORD flags);
103 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSStartRemoteControlSessionExA(LPSTR pTargetServerName,
104                                                                   ULONG TargetLogonId,
105                                                                   BYTE HotkeyVk,
106                                                                   USHORT HotkeyModifiers,
107                                                                   DWORD flags);
108 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSStopRemoteControlSession(ULONG LogonId);
109 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSConnectSessionW(ULONG LogonId, ULONG TargetLogonId,
110                                                      PWSTR pPassword, BOOL bWait);
111 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSConnectSessionA(ULONG LogonId, ULONG TargetLogonId,
112                                                      PSTR pPassword, BOOL bWait);
113 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateServersW(LPWSTR pDomainName, DWORD Reserved,
114                                                        DWORD Version,
115                                                        PWTS_SERVER_INFOW* ppServerInfo,
116                                                        DWORD* pCount);
117 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateServersA(LPSTR pDomainName, DWORD Reserved,
118                                                        DWORD Version,
119                                                        PWTS_SERVER_INFOA* ppServerInfo,
120                                                        DWORD* pCount);
121 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSOpenServerW(LPWSTR pServerName);
122 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSOpenServerA(LPSTR pServerName);
123 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSOpenServerExW(LPWSTR pServerName);
124 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSOpenServerExA(LPSTR pServerName);
125 FREERDP_LOCAL VOID WINAPI FreeRDP_WTSCloseServer(HANDLE hServer);
126 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateSessionsW(HANDLE hServer, DWORD Reserved,
127                                                         DWORD Version,
128                                                         PWTS_SESSION_INFOW* ppSessionInfo,
129                                                         DWORD* pCount);
130 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateSessionsA(HANDLE hServer, DWORD Reserved,
131                                                         DWORD Version,
132                                                         PWTS_SESSION_INFOA* ppSessionInfo,
133                                                         DWORD* pCount);
134 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateSessionsExW(HANDLE hServer, DWORD* pLevel,
135                                                           DWORD Filter,
136                                                           PWTS_SESSION_INFO_1W* ppSessionInfo,
137                                                           DWORD* pCount);
138 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateSessionsExA(HANDLE hServer, DWORD* pLevel,
139                                                           DWORD Filter,
140                                                           PWTS_SESSION_INFO_1A* ppSessionInfo,
141                                                           DWORD* pCount);
142 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateProcessesW(HANDLE hServer, DWORD Reserved,
143                                                          DWORD Version,
144                                                          PWTS_PROCESS_INFOW* ppProcessInfo,
145                                                          DWORD* pCount);
146 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateProcessesA(HANDLE hServer, DWORD Reserved,
147                                                          DWORD Version,
148                                                          PWTS_PROCESS_INFOA* ppProcessInfo,
149                                                          DWORD* pCount);
150 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSTerminateProcess(HANDLE hServer, DWORD ProcessId,
151                                                       DWORD ExitCode);
152 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQuerySessionInformationW(HANDLE hServer, DWORD SessionId,
153                                                               WTS_INFO_CLASS WTSInfoClass,
154                                                               LPWSTR* ppBuffer,
155                                                               DWORD* pBytesReturned);
156 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQuerySessionInformationA(HANDLE hServer, DWORD SessionId,
157                                                               WTS_INFO_CLASS WTSInfoClass,
158                                                               LPSTR* ppBuffer,
159                                                               DWORD* pBytesReturned);
160 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQueryUserConfigW(LPWSTR pServerName, LPWSTR pUserName,
161                                                       WTS_CONFIG_CLASS WTSConfigClass,
162                                                       LPWSTR* ppBuffer, DWORD* pBytesReturned);
163 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQueryUserConfigA(LPSTR pServerName, LPSTR pUserName,
164                                                       WTS_CONFIG_CLASS WTSConfigClass,
165                                                       LPSTR* ppBuffer, DWORD* pBytesReturned);
166 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSetUserConfigW(LPWSTR pServerName, LPWSTR pUserName,
167                                                     WTS_CONFIG_CLASS WTSConfigClass, LPWSTR pBuffer,
168                                                     DWORD DataLength);
169 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSetUserConfigA(LPSTR pServerName, LPSTR pUserName,
170                                                     WTS_CONFIG_CLASS WTSConfigClass, LPSTR pBuffer,
171                                                     DWORD DataLength);
172 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSendMessageW(HANDLE hServer, DWORD SessionId, LPWSTR pTitle,
173                                                   DWORD TitleLength, LPWSTR pMessage,
174                                                   DWORD MessageLength, DWORD Style, DWORD Timeout,
175                                                   DWORD* pResponse, BOOL bWait);
176 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSendMessageA(HANDLE hServer, DWORD SessionId, LPSTR pTitle,
177                                                   DWORD TitleLength, LPSTR pMessage,
178                                                   DWORD MessageLength, DWORD Style, DWORD Timeout,
179                                                   DWORD* pResponse, BOOL bWait);
180 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSDisconnectSession(HANDLE hServer, DWORD SessionId, BOOL bWait);
181 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSLogoffSession(HANDLE hServer, DWORD SessionId, BOOL bWait);
182 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSShutdownSystem(HANDLE hServer, DWORD ShutdownFlag);
183 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSWaitSystemEvent(HANDLE hServer, DWORD EventMask,
184                                                      DWORD* pEventFlags);
185 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId,
186                                                           LPSTR pVirtualName);
187 FREERDP_LOCAL HANDLE WINAPI FreeRDP_WTSVirtualChannelOpenEx(DWORD SessionId, LPSTR pVirtualName,
188                                                             DWORD flags);
189 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelClose(HANDLE hChannelHandle);
190 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelRead(HANDLE hChannelHandle, ULONG TimeOut,
191                                                         PCHAR Buffer, ULONG BufferSize,
192                                                         PULONG pBytesRead);
193 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer,
194                                                          ULONG Length, PULONG pBytesWritten);
195 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeInput(HANDLE hChannelHandle);
196 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelPurgeOutput(HANDLE hChannelHandle);
197 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSVirtualChannelQuery(HANDLE hChannelHandle,
198                                                          WTS_VIRTUAL_CLASS WtsVirtualClass,
199                                                          PVOID* ppBuffer, DWORD* pBytesReturned);
200 FREERDP_LOCAL VOID WINAPI FreeRDP_WTSFreeMemory(PVOID pMemory);
201 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSFreeMemoryExW(WTS_TYPE_CLASS WTSTypeClass, PVOID pMemory,
202                                                    ULONG NumberOfEntries);
203 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSFreeMemoryExA(WTS_TYPE_CLASS WTSTypeClass, PVOID pMemory,
204                                                    ULONG NumberOfEntries);
205 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSRegisterSessionNotification(HWND hWnd, DWORD dwFlags);
206 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotification(HWND hWnd);
207 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSRegisterSessionNotificationEx(HANDLE hServer, HWND hWnd,
208                                                                    DWORD dwFlags);
209 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSUnRegisterSessionNotificationEx(HANDLE hServer, HWND hWnd);
210 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQueryUserToken(ULONG SessionId, PHANDLE phToken);
211 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateProcessesExW(HANDLE hServer, DWORD* pLevel,
212                                                            DWORD SessionId, LPWSTR* ppProcessInfo,
213                                                            DWORD* pCount);
214 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateProcessesExA(HANDLE hServer, DWORD* pLevel,
215                                                            DWORD SessionId, LPSTR* ppProcessInfo,
216                                                            DWORD* pCount);
217 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateListenersW(HANDLE hServer, PVOID pReserved,
218                                                          DWORD Reserved,
219                                                          PWTSLISTENERNAMEW pListeners,
220                                                          DWORD* pCount);
221 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSEnumerateListenersA(HANDLE hServer, PVOID pReserved,
222                                                          DWORD Reserved,
223                                                          PWTSLISTENERNAMEA pListeners,
224                                                          DWORD* pCount);
225 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQueryListenerConfigW(HANDLE hServer, PVOID pReserved,
226                                                           DWORD Reserved, LPWSTR pListenerName,
227                                                           PWTSLISTENERCONFIGW pBuffer);
228 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSQueryListenerConfigA(HANDLE hServer, PVOID pReserved,
229                                                           DWORD Reserved, LPSTR pListenerName,
230                                                           PWTSLISTENERCONFIGA pBuffer);
231 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSCreateListenerW(HANDLE hServer, PVOID pReserved,
232                                                      DWORD Reserved, LPWSTR pListenerName,
233                                                      PWTSLISTENERCONFIGW pBuffer, DWORD flag);
234 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSCreateListenerA(HANDLE hServer, PVOID pReserved,
235                                                      DWORD Reserved, LPSTR pListenerName,
236                                                      PWTSLISTENERCONFIGA pBuffer, DWORD flag);
237 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSetListenerSecurityW(HANDLE hServer, PVOID pReserved,
238                                                           DWORD Reserved, LPWSTR pListenerName,
239                                                           SECURITY_INFORMATION SecurityInformation,
240                                                           PSECURITY_DESCRIPTOR pSecurityDescriptor);
241 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSSetListenerSecurityA(HANDLE hServer, PVOID pReserved,
242                                                           DWORD Reserved, LPSTR pListenerName,
243                                                           SECURITY_INFORMATION SecurityInformation,
244                                                           PSECURITY_DESCRIPTOR pSecurityDescriptor);
245 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSGetListenerSecurityW(HANDLE hServer, PVOID pReserved,
246                                                           DWORD Reserved, LPWSTR pListenerName,
247                                                           SECURITY_INFORMATION SecurityInformation,
248                                                           PSECURITY_DESCRIPTOR pSecurityDescriptor,
249                                                           DWORD nLength, LPDWORD lpnLengthNeeded);
250 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSGetListenerSecurityA(HANDLE hServer, PVOID pReserved,
251                                                           DWORD Reserved, LPSTR pListenerName,
252                                                           SECURITY_INFORMATION SecurityInformation,
253                                                           PSECURITY_DESCRIPTOR pSecurityDescriptor,
254                                                           DWORD nLength, LPDWORD lpnLengthNeeded);
255 
256 FREERDP_LOCAL BOOL CDECL FreeRDP_WTSEnableChildSessions(BOOL bEnable);
257 FREERDP_LOCAL BOOL CDECL FreeRDP_WTSIsChildSessionsEnabled(PBOOL pbEnabled);
258 FREERDP_LOCAL BOOL CDECL FreeRDP_WTSGetChildSessionId(PULONG pSessionId);
259 
260 FREERDP_LOCAL DWORD WINAPI FreeRDP_WTSGetActiveConsoleSessionId(void);
261 
262 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSLogoffUser(HANDLE hServer);
263 FREERDP_LOCAL BOOL WINAPI FreeRDP_WTSLogonUser(HANDLE hServer, LPCSTR username, LPCSTR password,
264                                                LPCSTR domain);
265 
266 #endif /* FREERDP_LIB_CORE_SERVER_H */
267