FreeRDP
Loading...
Searching...
No Matches
lsa.c
1
20
21#include <winpr/config.h>
22
23#ifndef _WIN32
24
25#include <winpr/crt.h>
26#include <winpr/assert.h>
27#include <winpr/nt.h>
28#include <winpr/secapi.h>
29
30#include "lsa_proto.h"
31#include "lsa_transport.h"
32
33typedef struct
34{
35 int fd;
36 UINT32 nextRequestId;
37 UINT64 daemonInstanceId;
38} LsaConnection;
39
40NTSTATUS LsaConnectUntrusted(PHANDLE LsaHandle)
41{
42 if (!LsaHandle)
43 return STATUS_INVALID_PARAMETER;
44 *LsaHandle = nullptr;
45
46 const int fd = lsa4posix_client_connect();
47 if (fd < 0)
48 return STATUS_NO_SUCH_PACKAGE;
49
50 LsaConnection* conn = (LsaConnection*)calloc(1, sizeof(LsaConnection));
51 if (!conn)
52 {
53 lsa4posix_client_close(fd);
54 return STATUS_NO_MEMORY;
55 }
56 conn->fd = fd;
57 conn->nextRequestId = 1;
58
59 const UINT32 requestId = conn->nextRequestId++;
60 LSA_MSG_HDR hdr = { LSA_MSG_HELLO, 0, requestId };
61 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
62 wStream* request = nullptr;
63 if (!lsa_proto_writer_init(&w, &hdr) || !lsa_proto_write_u16(&w, LSA_PROTO_VERSION_MAJOR) ||
64 !lsa_proto_write_u16(&w, LSA_PROTO_VERSION_MINOR) || !lsa_proto_write_u32(&w, 0) ||
65 !lsa_proto_writer_finish(&w, &request))
66 {
67 lsa_proto_writer_abort(&w);
68 lsa4posix_client_close(fd);
69 free(conn);
70 return STATUS_UNSUCCESSFUL;
71 }
72
73 LsaProtoReader reader = WINPR_C_ARRAY_INIT;
74 LSA_MSG_HDR respHdr = WINPR_C_ARRAY_INIT;
75 wStream* response = nullptr;
76 NTSTATUS status =
77 lsa4posix_transact(fd, LSA_MSG_HELLO, request, requestId, &reader, &respHdr, &response);
78 if (status != STATUS_SUCCESS)
79 {
80 lsa4posix_client_close(fd);
81 free(conn);
82 return status;
83 }
84
85 UINT32 respStatus = 0;
86 UINT16 major = 0;
87 UINT16 minor = 0;
88 UINT32 serverFlags = 0;
89 if (!lsa_proto_read_u32(&reader, &respStatus) || !lsa_proto_read_u16(&reader, &major) ||
90 !lsa_proto_read_u16(&reader, &minor) || !lsa_proto_read_u32(&reader, &serverFlags) ||
91 !lsa_proto_read_u64(&reader, &conn->daemonInstanceId))
92 status = STATUS_UNSUCCESSFUL;
93 else if (respStatus != STATUS_SUCCESS)
94 status = (NTSTATUS)respStatus;
95
96 lsa_proto_reader_done(&reader);
97 Stream_Free(response, TRUE);
98
99 if (status != STATUS_SUCCESS)
100 {
101 lsa4posix_client_close(fd);
102 free(conn);
103 return status;
104 }
105
106 *LsaHandle = (HANDLE)conn;
107 return STATUS_SUCCESS;
108}
109
110NTSTATUS LsaLookupAuthenticationPackage(HANDLE LsaHandle, PLSA_STRING PackageName,
111 PULONG AuthenticationPackage)
112{
113 if (!LsaHandle || !PackageName || !AuthenticationPackage)
114 return STATUS_INVALID_PARAMETER;
115
116 LsaConnection* conn = (LsaConnection*)LsaHandle;
117 *AuthenticationPackage = 0;
118
119 const UINT32 requestId = conn->nextRequestId++;
120 LSA_MSG_HDR hdr = { LSA_MSG_LOOKUP_PACKAGE, 0, requestId };
121 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
122 wStream* request = nullptr;
123 if (!lsa_proto_writer_init(&w, &hdr) ||
124 !lsa_proto_write_blob(&w, (const BYTE*)PackageName->Buffer, PackageName->Length) ||
125 !lsa_proto_writer_finish(&w, &request))
126 {
127 lsa_proto_writer_abort(&w);
128 return STATUS_UNSUCCESSFUL;
129 }
130
131 LsaProtoReader reader = WINPR_C_ARRAY_INIT;
132 LSA_MSG_HDR respHdr = WINPR_C_ARRAY_INIT;
133 wStream* response = nullptr;
134 NTSTATUS status = lsa4posix_transact(conn->fd, LSA_MSG_LOOKUP_PACKAGE, request, requestId,
135 &reader, &respHdr, &response);
136 if (status != STATUS_SUCCESS)
137 return status;
138
139 UINT32 respStatus = 0;
140 UINT32 packageId = 0;
141 if (!lsa_proto_read_u32(&reader, &respStatus) || !lsa_proto_read_u32(&reader, &packageId))
142 status = STATUS_UNSUCCESSFUL;
143 else if (respStatus != STATUS_SUCCESS)
144 status = (NTSTATUS)respStatus;
145 else
146 *AuthenticationPackage = packageId;
147
148 lsa_proto_reader_done(&reader);
149 Stream_Free(response, TRUE);
150 return status;
151}
152
153NTSTATUS LsaCallAuthenticationPackage(HANDLE LsaHandle, ULONG AuthenticationPackage,
154 PVOID ProtocolSubmitBuffer, ULONG SubmitBufferLength,
155 PVOID* ProtocolReturnBuffer, PULONG ReturnBufferLength,
156 PNTSTATUS ProtocolStatus)
157{
158 if (!LsaHandle || (!ProtocolSubmitBuffer && (SubmitBufferLength > 0)))
159 return STATUS_INVALID_PARAMETER;
160
161 LsaConnection* conn = (LsaConnection*)LsaHandle;
162 if (ProtocolReturnBuffer)
163 *ProtocolReturnBuffer = nullptr;
164 if (ReturnBufferLength)
165 *ReturnBufferLength = 0;
166 if (ProtocolStatus)
167 *ProtocolStatus = STATUS_SUCCESS;
168
169 const UINT32 requestId = conn->nextRequestId++;
170 LSA_MSG_HDR hdr = { LSA_MSG_CALL_PACKAGE, 0, requestId };
171 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
172 wStream* request = nullptr;
173 if (!lsa_proto_writer_init(&w, &hdr) || !lsa_proto_write_u32(&w, AuthenticationPackage) ||
174 !lsa_proto_write_blob(&w, (const BYTE*)ProtocolSubmitBuffer, SubmitBufferLength) ||
175 !lsa_proto_writer_finish(&w, &request))
176 {
177 lsa_proto_writer_abort(&w);
178 return STATUS_UNSUCCESSFUL;
179 }
180
181 LsaProtoReader reader = WINPR_C_ARRAY_INIT;
182 LSA_MSG_HDR respHdr = WINPR_C_ARRAY_INIT;
183 wStream* response = nullptr;
184 NTSTATUS status = lsa4posix_transact(conn->fd, LSA_MSG_CALL_PACKAGE, request, requestId,
185 &reader, &respHdr, &response);
186 if (status != STATUS_SUCCESS)
187 return status;
188
189 UINT32 respStatus = 0;
190 UINT32 protocolStatus = 0;
191 BYTE* ret = nullptr;
192 UINT32 retLen = 0;
193 if (!lsa_proto_read_u32(&reader, &respStatus) ||
194 !lsa_proto_read_u32(&reader, &protocolStatus) ||
195 !lsa_proto_read_blob(&reader, &ret, &retLen))
196 status = STATUS_UNSUCCESSFUL;
197 else if (respStatus != STATUS_SUCCESS)
198 status = (NTSTATUS)respStatus;
199 else
200 {
201 if (ProtocolStatus)
202 *ProtocolStatus = (NTSTATUS)protocolStatus;
203 if (ProtocolReturnBuffer)
204 *ProtocolReturnBuffer = ret;
205 else
206 free(ret);
207 if (ReturnBufferLength)
208 *ReturnBufferLength = retLen;
209 ret = nullptr;
210 }
211
212 free(ret);
213 lsa_proto_reader_done(&reader);
214 Stream_Free(response, TRUE);
215 return status;
216}
217
218NTSTATUS LsaFreeReturnBuffer(PVOID Buffer)
219{
220 free(Buffer);
221 return STATUS_SUCCESS;
222}
223
224NTSTATUS LsaDeregisterLogonProcess(HANDLE LsaHandle)
225{
226 LsaConnection* conn = (LsaConnection*)LsaHandle;
227 if (!conn)
228 return STATUS_INVALID_HANDLE;
229
230 lsa4posix_client_close(conn->fd);
231 free(conn);
232 return STATUS_SUCCESS;
233}
234
235ULONG LsaNtStatusToWinError(NTSTATUS Status)
236{
237 switch (Status)
238 {
239 case STATUS_SUCCESS:
240 return ERROR_SUCCESS;
241 case STATUS_NO_SUCH_PACKAGE:
242 return ERROR_NO_SUCH_PACKAGE;
243 case STATUS_NO_MEMORY:
244 return ERROR_NOT_ENOUGH_MEMORY;
245 case STATUS_INVALID_PARAMETER:
246 return ERROR_INVALID_PARAMETER;
247 case STATUS_INVALID_HANDLE:
248 return ERROR_INVALID_HANDLE;
249 default:
250 return ERROR_GEN_FAILURE;
251 }
252}
253
254#endif /* _WIN32 */