ReactOS  0.4.14-dev-114-gc8cbd56
smclient.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Windows-Compatible Session Manager
3  * LICENSE: BSD 2-Clause License
4  * FILE: lib/smlib/smclient.c
5  * PURPOSE: SMSS Client Library Stubs for calling SM APIs from a client
6  * PROGRAMMERS: Alex Ionescu
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "precomp.h"
12 #include <sm/smmsg.h> // To go in precomp.h after
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* FUNCTIONS ******************************************************************/
18 
20 NTAPI
22  IN PRTL_USER_PROCESS_INFORMATION ProcessInformation,
23  IN BOOLEAN DebugFlag)
24 {
26  SM_API_MSG SmApiMsg;
27 
28 #if 0 //def _WIN64 // You can take care of this Timo
29  /* 64-bit SMSS needs to talk to 32-bit processes so do the LPC conversion */
30  if (SmpIsWow64Process())
31  {
32  return SmpWow64ExecPgm(SmApiPort, ProcessInformation, DebugFlag);
33  }
34 #endif
35 
36  /* Initialize the generic LPC header */
37  SmApiMsg.h.u2.ZeroInit = 0;
38  SmApiMsg.h.u1.s1.DataLength = sizeof(SM_EXEC_PGM_MSG) + 8;
39  SmApiMsg.h.u1.s1.TotalLength = sizeof(SmApiMsg);
40 
41  /* Initialize this specific API's parameters */
42  SmApiMsg.ApiNumber = SmpExecPgmApi;
44  ProcessInformation,
45  sizeof(SmApiMsg.u.ExecPgm.ProcessInformation));
46  SmApiMsg.u.ExecPgm.DebugFlag = DebugFlag;
47 
48  /* Send the message to SMSS */
49  Status = NtRequestWaitReplyPort(SmApiPort, &SmApiMsg.h, &SmApiMsg.h);
50  if (!NT_SUCCESS(Status))
51  {
52  DPRINT1("SmExecPgm: NtRequestWaitReply Failed %lx\n", Status);
53  }
54  else
55  {
56  /* Upon success, we use the API's return value */
57  Status = SmApiMsg.ReturnValue;
58  }
59 
60  /* Close the handles that the parent passed in and return status */
61  NtClose(ProcessInformation->ProcessHandle);
62  NtClose(ProcessInformation->ThreadHandle);
63  return Status;
64 }
65 
67 NTAPI
69  IN HANDLE SbApiPort,
72 {
74  SB_CONNECTION_INFO ConnectInfo;
76  SECURITY_QUALITY_OF_SERVICE SecurityQos;
77  ULONG ConnectInfoLength = sizeof(ConnectInfo);
78 
79  /* Setup the QoS structure */
82  SecurityQos.EffectiveOnly = TRUE;
83 
84  /* Set the SM API port name */
85  RtlInitUnicodeString(&PortName, L"\\SmApiPort");
86 
87  /* Check if this is a client connecting to SMSS, or SMSS to itself */
88  if (SbApiPortName)
89  {
90  /* A client SB port as well as an image type must be present */
91  if (!(SbApiPort) || !(ImageType)) return STATUS_INVALID_PARAMETER_MIX;
92 
93  /* Copy the client port name, and NULL-terminate it */
94  RtlCopyMemory(ConnectInfo.SbApiPortName,
95  SbApiPortName->Buffer,
96  SbApiPortName->Length);
97  ConnectInfo.SbApiPortName[SbApiPortName->Length /
98  sizeof(WCHAR)] = UNICODE_NULL;
99 
100  /* Save the subsystem type */
101  ConnectInfo.SubsystemType = ImageType;
102  }
103  else
104  {
105  /* No client port, and the subsystem type is not set */
106  ConnectInfo.SbApiPortName[0] = UNICODE_NULL;
108  }
109 
110  /* Connect to SMSS and exchange connection information */
112  &PortName,
113  &SecurityQos,
114  NULL,
115  NULL,
116  NULL,
117  &ConnectInfo,
118  &ConnectInfoLength);
119  if (!NT_SUCCESS(Status))
120  {
121  DPRINT1("SmConnectToSm: Connect to Sm failed %lx\n", Status);
122  }
123  else
124  {
125  /* Treat a warning or informational status as success */
127  }
128 
129  /* Return if the connection was successful or not */
130  return Status;
131 }
132 
133 NTSTATUS
134 NTAPI
137  IN NTSTATUS SessionStatus)
138 {
140  SM_API_MSG ApiMessage;
141  PSM_SESSION_COMPLETE_MSG SessionComplete = &ApiMessage.u.SessionComplete;
142 
143  /* Set the message data */
144  SessionComplete->SessionId = SessionId;
145  SessionComplete->SessionStatus = SessionStatus;
146 
147  /* Set the API Message Port Message header */
148  ApiMessage.ApiNumber = SmpSessionCompleteApi;
149  ApiMessage.h.u1.s1.DataLength = sizeof(SM_SESSION_COMPLETE_MSG) + 8;
150  ApiMessage.h.u1.s1.TotalLength = sizeof(SM_API_MSG);
151  ApiMessage.h.u2.ZeroInit = 0;
152 
153  /* Sent the message and wait for a reply */
155  &ApiMessage.h,
156  &ApiMessage.h);
157  if (NT_SUCCESS(Status))
158  {
159  /* Return the real status */
160  Status = ApiMessage.ReturnValue;
161  }
162  else
163  {
164  DPRINT1("SmCompleteSession: NtRequestWaitReply failed\n");
165  }
166 
167  /* Return status */
168  return Status;
169 }
PORT_MESSAGE h
Definition: smmsg.h:92
SM_EXEC_PGM_MSG ExecPgm
Definition: smmsg.h:100
NTSTATUS NTAPI SmExecPgm(IN HANDLE SmApiPort, IN PRTL_USER_PROCESS_INFORMATION ProcessInformation, IN BOOLEAN DebugFlag)
Definition: smclient.c:21
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
ULONG SubsystemType
Definition: smmsg.h:193
RTL_USER_PROCESS_INFORMATION ProcessInformation
Definition: smmsg.h:60
HANDLE SmApiPort
Definition: smss.c:23
NTSTATUS NTAPI NtRequestWaitReplyPort(IN HANDLE PortHandle, IN PPORT_MESSAGE LpcRequest, IN OUT PPORT_MESSAGE LpcReply)
Definition: send.c:695
ULONG SessionId
Definition: dllmain.c:28
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_INVALID_PARAMETER_MIX
Definition: ntstatus.h:271
NTSTATUS NTAPI SmConnectToSm(IN PUNICODE_STRING SbApiPortName, IN HANDLE SbApiPort, IN ULONG ImageType, OUT PHANDLE SmApiPort)
Definition: smclient.c:68
union _SM_API_MSG::@3381 u
SECURITY_CONTEXT_TRACKING_MODE ContextTrackingMode
Definition: lsa.idl:66
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define UNICODE_NULL
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
SMSRV_API_NUMBER ApiNumber
Definition: smmsg.h:93
NTSTATUS NTAPI NtConnectPort(OUT PHANDLE PortHandle, IN PUNICODE_STRING PortName, IN PSECURITY_QUALITY_OF_SERVICE SecurityQos, IN OUT PPORT_VIEW ClientView OPTIONAL, IN OUT PREMOTE_PORT_VIEW ServerView OPTIONAL, OUT PULONG MaxMessageLength OPTIONAL, IN OUT PVOID ConnectionInformation OPTIONAL, IN OUT PULONG ConnectionInformationLength OPTIONAL)
Definition: connect.c:753
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define IMAGE_SUBSYSTEM_UNKNOWN
Definition: ntimage.h:435
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
WCHAR SbApiPortName[120]
Definition: smmsg.h:194
#define SECURITY_DYNAMIC_TRACKING
Definition: setypes.h:103
static const WCHAR L[]
Definition: oid.c:1250
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:414
NTSTATUS NTAPI SmSessionComplete(IN HANDLE SmApiPort, IN ULONG SessionId, IN NTSTATUS SessionStatus)
Definition: smclient.c:135
Status
Definition: gdiplustypes.h:24
BOOLEAN DebugFlag
Definition: smmsg.h:61
struct _SM_API_MSG SM_API_MSG
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
Definition: lsa.idl:65
struct _SM_SESSION_COMPLETE_MSG SM_SESSION_COMPLETE_MSG
#define DPRINT1
Definition: precomp.h:8
ImageType
Definition: gdiplusenums.h:191
struct _SM_EXEC_PGM_MSG SM_EXEC_PGM_MSG
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
SM_SESSION_COMPLETE_MSG SessionComplete
Definition: smmsg.h:98
return STATUS_SUCCESS
Definition: btrfs.c:2966
NTSTATUS SessionStatus
Definition: smmsg.h:50
NTSTATUS ReturnValue
Definition: smmsg.h:94
IN PUNICODE_STRING PortName
Definition: conport.c:35