xref: /reactos/sdk/include/reactos/subsys/sm/smmsg.h (revision 2dddbd5c)
1 /*
2  * PROJECT:     ReactOS NT-Compatible Session Manager
3  * LICENSE:     BSD 2-Clause License (https://spdx.org/licenses/BSD-2-Clause)
4  * PURPOSE:     SMSS Client (SB and SM) Message Format
5  * COPYRIGHT:   Copyright 2012-2013 Alex Ionescu <alex.ionescu@reactos.org>
6  *              Copyright 2021 Hervé Poussineau <hpoussin@reactos.org>
7  *              Copyright 2022 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
8  */
9 
10 #ifndef _SM_MSG_
11 #define _SM_MSG_
12 
13 #pragma once
14 
15 //
16 // There are the APIs that a Client (such as CSRSS) can send to the SMSS Server.
17 // These are called "SM" APIs.
18 //
19 // The exact names are not known, but we are basing them on the SmpApiName array
20 // in the checked build of SMSS, which is probably a close approximation. We add
21 // "p" to use the similar nomenclature seen/leaked out in the Base CSRSS APIs.
22 //
23 // The enumeration finishes with an enumeratee holding the maximum API number.
24 // Its name is based on BasepMaxApiNumber, UserpMaxApiNumber...
25 //
26 typedef enum _SMSRV_API_NUMBER
27 {
28     SmpCreateForeignSessionApi,
29     SmpSessionCompleteApi,
30     SmpTerminateForeignSessionApi,
31     SmpExecPgmApi,
32     SmpLoadDeferedSubsystemApi,
33     SmpStartCsrApi,
34     SmpStopCsrApi,
35 
36     SmpMaxApiNumber
37 } SMSRV_API_NUMBER;
38 
39 //
40 // These are the structures making up the SM_API_MSG packet structure defined
41 // below. Each one corresponds to an equivalent API from the list above.
42 //
43 typedef struct _SM_CREATE_FOREIGN_SESSION_MSG
44 {
45     ULONG NotImplemented;
46 } SM_CREATE_FOREIGN_SESSION_MSG, *PSM_CREATE_FOREIGN_SESSION_MSG;
47 
48 typedef struct _SM_SESSION_COMPLETE_MSG
49 {
50     ULONG SessionId;
51     NTSTATUS SessionStatus;
52 } SM_SESSION_COMPLETE_MSG, *PSM_SESSION_COMPLETE_MSG;
53 
54 typedef struct _SM_TERMINATE_FOREIGN_SESSION_MSG
55 {
56     ULONG NotImplemented;
57 } SM_TERMINATE_FOREIGN_SESSION_MSG, *PSM_TERMINATE_FOREIGN_SESSION_MSG;
58 
59 typedef struct _SM_EXEC_PGM_MSG
60 {
61     RTL_USER_PROCESS_INFORMATION ProcessInformation;
62     BOOLEAN DebugFlag;
63 } SM_EXEC_PGM_MSG, *PSM_EXEC_PGM_MSG;
64 #ifndef _WIN64
65 C_ASSERT(sizeof(SM_EXEC_PGM_MSG) == 0x48);
66 #else
67 C_ASSERT(sizeof(SM_EXEC_PGM_MSG) == 0x70);
68 #endif
69 
70 typedef struct _SM_LOAD_DEFERED_SUBSYSTEM_MSG
71 {
72     ULONG Length;
73     WCHAR Buffer[32];
74 } SM_LOAD_DEFERED_SUBSYSTEM_MSG, *PSM_LOAD_DEFERED_SUBSYSTEM_MSG;
75 
76 typedef struct _SM_START_CSR_MSG
77 {
78     ULONG MuSessionId;
79     ULONG Length;
80     WCHAR Buffer[128];
81     HANDLE WindowsSubSysProcessId;
82     HANDLE SmpInitialCommandProcessId;
83 } SM_START_CSR_MSG, *PSM_START_CSR_MSG;
84 #ifndef _WIN64
85 C_ASSERT(sizeof(SM_START_CSR_MSG) == 0x110);
86 #else
87 C_ASSERT(sizeof(SM_START_CSR_MSG) == 0x118);
88 #endif
89 
90 typedef struct _SM_STOP_CSR_MSG
91 {
92     ULONG MuSessionId;
93 } SM_STOP_CSR_MSG, *PSM_STOP_CSR_MSG;
94 
95 #if defined(__REACTOS__) && DBG
96 #include "smrosdbg.h"
97 #endif
98 
99 //
100 // This is the actual packet structure sent over LPC to the \SmApiPort
101 //
102 typedef struct _SM_API_MSG
103 {
104     PORT_MESSAGE h;
105     SMSRV_API_NUMBER ApiNumber;
106     NTSTATUS ReturnValue;
107     union
108     {
109         SM_CREATE_FOREIGN_SESSION_MSG CreateForeignSession;
110         SM_SESSION_COMPLETE_MSG SessionComplete;
111         SM_TERMINATE_FOREIGN_SESSION_MSG TerminateForeignComplete;
112         SM_EXEC_PGM_MSG ExecPgm;
113         SM_LOAD_DEFERED_SUBSYSTEM_MSG LoadDefered;
114         SM_START_CSR_MSG StartCsr;
115         SM_STOP_CSR_MSG StopCsr;
116 
117 #if defined(__REACTOS__) && DBG
118         SM_QUERYINFO_MSG QueryInfo;
119 #endif
120     } u;
121 } SM_API_MSG, *PSM_API_MSG;
122 
123 //
124 // This is the size that Server 2003 SP1 SMSS expects, so make sure we conform.
125 //
126 #ifndef _WIN64
127 C_ASSERT(sizeof(SM_API_MSG) == 0x130);
128 #else
129 C_ASSERT(sizeof(SM_API_MSG) == 0x148);
130 #endif
131 
132 //
133 // There are the APIs that the SMSS Server can send to a client (such as CSRSS).
134 // These are called "SB" APIs.
135 //
136 // The exact names are unknown but we are basing them on the CsrServerSbApiName
137 // array in the checked build of CSRSRV which is probably a close approximation.
138 // We add "p" to use the similar nomenclature seen/leaked out in the Base CSRSS
139 // APIs.
140 //
141 // The enumeration finishes with an enumeratee holding the maximum API number.
142 // Its name is based on BasepMaxApiNumber, UserpMaxApiNumber...
143 //
144 typedef enum _SB_API_NUMBER
145 {
146     SbpCreateSession,
147     SbpTerminateSession,
148     SbpForeignSessionComplete,
149     SbpCreateProcess,
150 
151     SbpMaxApiNumber
152 } SB_API_NUMBER;
153 
154 //
155 // These are the structures making up the SB_API_MSG packet structure defined
156 // below. Each one corresponds to an equivalent API from the list above.
157 //
158 typedef struct _SB_CREATE_SESSION_MSG
159 {
160     ULONG SessionId;
161     RTL_USER_PROCESS_INFORMATION ProcessInfo;
162     PVOID Reserved;
163     ULONG DbgSessionId;
164     CLIENT_ID DbgUiClientId;
165 } SB_CREATE_SESSION_MSG, *PSB_CREATE_SESSION_MSG;
166 #ifndef _WIN64
167 C_ASSERT(sizeof(SB_CREATE_SESSION_MSG) == 0x58);
168 #else
169 C_ASSERT(sizeof(SB_CREATE_SESSION_MSG) == 0x90);
170 #endif
171 
172 typedef struct _SB_TERMINATE_SESSION_MSG
173 {
174     ULONG SessionId;
175 } SB_TERMINATE_SESSION_MSG, *PSB_TERMINATE_SESSION_MSG;
176 
177 typedef struct _SB_FOREIGN_SESSION_COMPLETE_MSG
178 {
179     ULONG SessionId;
180 } SB_FOREIGN_SESSION_COMPLETE_MSG, *PSB_FOREIGN_SESSION_COMPLETE_MSG;
181 
182 #define SB_PROCESS_FLAGS_DEBUG          0x1
183 #define SB_PROCESS_FLAGS_WAIT_ON_THREAD 0x2
184 #define SB_PROCESS_FLAGS_RESERVE_1MB    0x8
185 #define SB_PROCESS_FLAGS_SKIP_CHECKS    0x20
186 typedef struct _SB_CREATE_PROCESS_MSG
187 {
188     union
189     {
190         struct
191         {
192             PUNICODE_STRING ImageName;
193             PUNICODE_STRING CurrentDirectory;
194             PUNICODE_STRING CommandLine;
195             PUNICODE_STRING DllPath;
196             ULONG Flags;
197             ULONG DebugFlags;
198         } In;
199         struct
200         {
201             HANDLE ProcessHandle;
202             HANDLE ThreadHandle;
203             ULONG SubsystemType;
204             CLIENT_ID ClientId;
205         } Out;
206     };
207 } SB_CREATE_PROCESS_MSG, *PSB_CREATE_PROCESS_MSG;
208 #ifndef _WIN64
209 C_ASSERT(sizeof(SB_CREATE_PROCESS_MSG) == 0x18);
210 #else
211 C_ASSERT(sizeof(SB_CREATE_PROCESS_MSG) == 0x28);
212 #endif
213 
214 #ifdef CreateProcess
215 #undef CreateProcess
216 #endif
217 
218 //
219 // When the server connects to a client, this structure is exchanged
220 //
221 typedef struct _SB_CONNECTION_INFO
222 {
223     ULONG SubsystemType;
224     WCHAR SbApiPortName[120];
225 } SB_CONNECTION_INFO, *PSB_CONNECTION_INFO;
226 
227 //
228 // This is the actual packet structure sent over LPC to the \SbApiPort
229 //
230 typedef struct _SB_API_MSG
231 {
232     PORT_MESSAGE h;
233     union
234     {
235         SB_CONNECTION_INFO ConnectionInfo;
236         struct
237         {
238             SB_API_NUMBER ApiNumber;
239             NTSTATUS ReturnValue;
240             union
241             {
242                 SB_CREATE_SESSION_MSG CreateSession;
243                 SB_TERMINATE_SESSION_MSG TerminateSession;
244                 SB_FOREIGN_SESSION_COMPLETE_MSG ForeignSessionComplete;
245                 SB_CREATE_PROCESS_MSG CreateProcess;
246             } u;
247         };
248     };
249 } SB_API_MSG, *PSB_API_MSG;
250 
251 //
252 // This is the size that Server 2003 SP1 SMSS expects, so make sure we conform.
253 //
254 #ifndef _WIN64
255 C_ASSERT(sizeof(SB_API_MSG) == 0x110);
256 #else
257 C_ASSERT(sizeof(SB_API_MSG) == 0x120);
258 #endif
259 
260 //
261 // SB Message Handler
262 //
263 typedef
264 BOOLEAN
265 (NTAPI *PSB_API_ROUTINE)(
266     _In_ PSB_API_MSG SbApiMsg
267 );
268 
269 //
270 // The actual server functions that a client linking with SMLIB can call.
271 //
272 /* NTDLL!RtlConnectToSm */
273 NTSTATUS
274 NTAPI
275 SmConnectToSm(
276     _In_opt_ PUNICODE_STRING SbApiPortName,
277     _In_opt_ HANDLE SbApiPort,
278     _In_opt_ ULONG ImageType,
279     _Out_ PHANDLE SmApiPort);
280 
281 /* NTDLL!RtlSendMsgToSm */
282 NTSTATUS
283 NTAPI
284 SmSendMsgToSm(
285     _In_ HANDLE SmApiPort,
286     _Inout_ PSM_API_MSG SmApiMsg);
287 
288 NTSTATUS
289 NTAPI
290 SmSessionComplete(
291     _In_ HANDLE SmApiPort,
292     _In_ ULONG SessionId,
293     _In_ NTSTATUS SessionStatus);
294 
295 NTSTATUS
296 NTAPI
297 SmExecPgm(
298     _In_ HANDLE SmApiPort,
299     _In_ PRTL_USER_PROCESS_INFORMATION ProcessInformation,
300     _In_ BOOLEAN DebugFlag);
301 
302 NTSTATUS
303 NTAPI
304 SmLoadDeferedSubsystem(
305     _In_ HANDLE SmApiPort,
306     _In_ PUNICODE_STRING DeferedSubsystem);
307 
308 NTSTATUS
309 NTAPI
310 SmStartCsr(
311     _In_ HANDLE SmApiPort,
312     _Out_ PULONG pMuSessionId,
313     _In_opt_ PUNICODE_STRING CommandLine,
314     _Out_ PHANDLE pWindowsSubSysProcessId,
315     _Out_ PHANDLE pInitialCommandProcessId);
316 
317 NTSTATUS
318 NTAPI
319 SmStopCsr(
320     _In_ HANDLE SmApiPort,
321     _In_ ULONG MuSessionId);
322 
323 #endif // _SM_MSG_
324