FreeRDP
Loading...
Searching...
No Matches
TestLsaClient.c
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <pthread.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/socket.h>
9#include <sys/un.h>
10
11#include <winpr/crt.h>
12#include <winpr/stream.h>
13#include <winpr/secapi.h>
14#include <winpr/nt.h>
15#include <winpr/environment.h>
16#include <winpr/path.h>
17
18#include "../lsa_proto.h"
19
20#define FAKE_PACKAGE_ID 0x2A
21
22#if !defined(__EMSCRIPTEN__)
23/* The transport test needs AF_UNIX sockets, which emscripten does not provide. */
24
25/* ---- tiny framing helpers mirroring the client (UINT32 big-endian length prefix) ---- */
26
27static BOOL rw_all(int fd, BYTE* data, size_t len, BOOL writing)
28{
29 size_t off = 0;
30 while (off < len)
31 {
32 const ssize_t n =
33 writing ? write(fd, &data[off], len - off) : read(fd, &data[off], len - off);
34 if (n > 0)
35 off += (size_t)n;
36 else
37 return FALSE;
38 }
39 return TRUE;
40}
41
42WINPR_ATTR_MALLOC(Stream_Free, 1)
43static wStream* frame_recv(int fd)
44{
45 BYTE pfx[4] = WINPR_C_ARRAY_INIT;
46 if (!rw_all(fd, pfx, sizeof(pfx), FALSE))
47 return nullptr;
48 /* the frame length, big-endian */
49 wStream pbuffer = WINPR_C_ARRAY_INIT;
50 wStream* ps = Stream_StaticConstInit(&pbuffer, pfx, sizeof(pfx));
51 UINT32 len = 0;
52 Stream_Read_UINT32_BE(ps, len);
53 if ((len == 0) || (len > LSA_PROTO_MAX_FRAME))
54 return nullptr;
55 wStream* s = Stream_New(nullptr, len);
56 if (!s)
57 return nullptr;
58 if (!rw_all(fd, Stream_Buffer(s), len, FALSE) || !Stream_SetLength(s, len) ||
59 !Stream_SetPosition(s, 0))
60 {
61 Stream_Free(s, TRUE);
62 return nullptr;
63 }
64 return s;
65}
66
67static BOOL frame_send(int fd, wStream* payload)
68{
69 const size_t len = Stream_Length(payload);
70 /* the frame length, big-endian */
71 BYTE pfx[4] = WINPR_C_ARRAY_INIT;
72 wStream pbuffer = WINPR_C_ARRAY_INIT;
73 wStream* ps = Stream_StaticInit(&pbuffer, pfx, sizeof(pfx));
74 Stream_Write_UINT32_BE(ps, (UINT32)len);
75 return rw_all(fd, pfx, sizeof(pfx), TRUE) && rw_all(fd, Stream_Buffer(payload), len, TRUE);
76}
77
78/* ---- fake lsa4posix daemon: one connection, serves HELLO/LOOKUP/CALL ---- */
79
80static void* fake_daemon(void* arg)
81{
82 const int listenFd = *(const int*)arg;
83 const int conn = accept(listenFd, nullptr, nullptr);
84 if (conn < 0)
85 return nullptr;
86
87 for (;;)
88 {
89 wStream* in = frame_recv(conn);
90 if (!in)
91 break;
92
93 LsaProtoReader r = WINPR_C_ARRAY_INIT;
94 LSA_MSG_HDR hdr = WINPR_C_ARRAY_INIT;
95 if (!lsa_proto_reader_init(&r, in, &hdr))
96 {
97 Stream_Free(in, TRUE);
98 break;
99 }
100
101 LSA_MSG_HDR rh = { hdr.type, LSA_MSG_FLAG_RESPONSE, hdr.requestId };
102 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
103 BOOL ok = lsa_proto_writer_init(&w, &rh);
104
105 if (ok && (hdr.type == LSA_MSG_HELLO))
106 {
107 UINT16 maj = 0;
108 UINT16 min = 0;
109 UINT32 cf = 0;
110 ok = lsa_proto_read_u16(&r, &maj) && lsa_proto_read_u16(&r, &min) &&
111 lsa_proto_read_u32(&r, &cf);
112 ok = ok && lsa_proto_write_u32(&w, STATUS_SUCCESS) &&
113 lsa_proto_write_u16(&w, LSA_PROTO_VERSION_MAJOR) &&
114 lsa_proto_write_u16(&w, LSA_PROTO_VERSION_MINOR) && lsa_proto_write_u32(&w, 0) &&
115 lsa_proto_write_u64(&w, 0x1122334455667788ULL);
116 }
117 else if (ok && (hdr.type == LSA_MSG_LOOKUP_PACKAGE))
118 {
119 BYTE* name = nullptr;
120 UINT32 nameLen = 0;
121 ok = lsa_proto_read_blob(&r, &name, &nameLen);
122 free(name);
123 ok = ok && lsa_proto_write_u32(&w, STATUS_SUCCESS) &&
124 lsa_proto_write_u32(&w, FAKE_PACKAGE_ID);
125 }
126 else if (ok && (hdr.type == LSA_MSG_CALL_PACKAGE))
127 {
128 UINT32 pkg = 0;
129 BYTE* submit = nullptr;
130 UINT32 submitLen = 0;
131 ok = lsa_proto_read_u32(&r, &pkg) && lsa_proto_read_blob(&r, &submit, &submitLen);
132 /* echo the submit buffer straight back as the return buffer */
133 ok = ok && lsa_proto_write_u32(&w, STATUS_SUCCESS) && lsa_proto_write_u32(&w, 0) &&
134 lsa_proto_write_blob(&w, submit, submitLen);
135 free(submit);
136 }
137 else
138 ok = FALSE;
139
140 lsa_proto_reader_done(&r);
141 Stream_Free(in, TRUE);
142
143 wStream* out = nullptr;
144 if (ok && lsa_proto_writer_finish(&w, &out))
145 {
146 const BOOL sent = frame_send(conn, out);
147 Stream_Free(out, TRUE);
148 if (!sent)
149 break;
150 }
151 else
152 {
153 lsa_proto_writer_abort(&w);
154 break;
155 }
156 }
157
158 close(conn);
159 return nullptr;
160}
161#endif
162
163static int test_codec_roundtrip(void)
164{
165 /* an odd-sized blob followed by a 64-bit field exercises the alignment after the blob */
166 const BYTE submit[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03 };
167 const UINT64 tail = 0x1122334455667788ULL;
168 LSA_MSG_HDR hdr = { LSA_MSG_CALL_PACKAGE, 0, 0x12345678 };
169 LsaProtoWriter w = WINPR_C_ARRAY_INIT;
170 wStream* framed = nullptr;
171 if (!lsa_proto_writer_init(&w, &hdr) || !lsa_proto_write_u32(&w, 0x99) ||
172 !lsa_proto_write_blob(&w, submit, sizeof(submit)) || !lsa_proto_write_u64(&w, tail) ||
173 !lsa_proto_writer_finish(&w, &framed))
174 {
175 lsa_proto_writer_abort(&w);
176 (void)fprintf(stderr, "codec: encode failed\n");
177 return -1;
178 }
179
180 /* NDR type serialization version 1, little-endian: the common header, then the private
181 * header, whose object length is the rest of the message and a multiple of 8 */
182 const BYTE commonHeader[] = { 0x01, 0x10, 0x08, 0x00, 0xcc, 0xcc, 0xcc, 0xcc };
183 const size_t total = Stream_Length(framed);
184 BYTE common[sizeof(commonHeader)] = WINPR_C_ARRAY_INIT;
185 UINT32 objectLength = 0;
186 if (total >= 16)
187 {
188 wStream sbuffer = WINPR_C_ARRAY_INIT;
189 wStream* hs = Stream_StaticConstInit(&sbuffer, Stream_Buffer(framed), total);
190 Stream_Read(hs, common, sizeof(common));
191 Stream_Read_UINT32(hs, objectLength);
192 }
193 if ((total < 16) || (memcmp(common, commonHeader, sizeof(common)) != 0) ||
194 (objectLength != total - 16) || ((objectLength % 8) != 0))
195 {
196 (void)fprintf(stderr, "codec: not an NDR type-serialized object\n");
197 Stream_Free(framed, TRUE);
198 return -1;
199 }
200
201 int rc = -1;
202 LsaProtoReader r = WINPR_C_ARRAY_INIT;
203 LSA_MSG_HDR got = WINPR_C_ARRAY_INIT;
204 UINT32 pkg = 0;
205 BYTE* blob = nullptr;
206 UINT32 blobLen = 0;
207 UINT64 gotTail = 0;
208 if (lsa_proto_reader_init(&r, framed, &got) && lsa_proto_read_u32(&r, &pkg) &&
209 lsa_proto_read_blob(&r, &blob, &blobLen) && lsa_proto_read_u64(&r, &gotTail))
210 {
211 if ((got.type == LSA_MSG_CALL_PACKAGE) && (got.requestId == 0x12345678) && (pkg == 0x99) &&
212 (blobLen == sizeof(submit)) && (memcmp(blob, submit, sizeof(submit)) == 0) &&
213 (gotTail == tail))
214 rc = 0;
215 else
216 (void)fprintf(stderr, "codec: field mismatch\n");
217 }
218 else
219 (void)fprintf(stderr, "codec: decode failed\n");
220 free(blob);
221 lsa_proto_reader_done(&r);
222 Stream_Free(framed, TRUE);
223 return rc;
224}
225
226#if !defined(__EMSCRIPTEN__)
227static int test_fake_daemon(void)
228{
229 int rc = -1;
230 int listenFd = -1;
231 pthread_t thread = 0;
232 BOOL threadStarted = FALSE;
233 BOOL dirCreated = FALSE;
234 HANDLE lsa = nullptr;
235 char* sockPath = nullptr;
236
237 char* tmp = GetKnownPath(KNOWN_PATH_TEMP);
238 char* dir = tmp ? GetCombinedPath(tmp, "lsatestXXXXXX") : nullptr;
239 free(tmp);
240 if (!dir || !mkdtemp(dir))
241 {
242 (void)fprintf(stderr, "creating the temporary socket directory failed\n");
243 goto cleanup;
244 }
245 dirCreated = TRUE;
246
247 struct sockaddr_un addr = WINPR_C_ARRAY_INIT;
248 sockPath = GetCombinedPath(dir, "lsa.sock");
249 if (!sockPath || (strlen(sockPath) >= sizeof(addr.sun_path)))
250 {
251 (void)fprintf(stderr, "socket path too long for AF_UNIX\n");
252 goto cleanup;
253 }
254
255 listenFd = socket(AF_UNIX, SOCK_STREAM, 0);
256 if (listenFd < 0)
257 {
258 (void)fprintf(stderr, "socket(AF_UNIX) failed\n");
259 goto cleanup;
260 }
261
262 addr.sun_family = AF_UNIX;
263 strncpy(addr.sun_path, sockPath, sizeof(addr.sun_path) - 1);
264 if ((bind(listenFd, (struct sockaddr*)&addr, sizeof(addr)) != 0) || (listen(listenFd, 1) != 0))
265 {
266 (void)fprintf(stderr, "bind/listen on %s failed\n", sockPath);
267 goto cleanup;
268 }
269
270 (void)SetEnvironmentVariableA("LSA4POSIX_SOCKET", sockPath);
271
272 if (pthread_create(&thread, nullptr, fake_daemon, &listenFd) != 0)
273 {
274 (void)fprintf(stderr, "pthread_create failed\n");
275 goto cleanup;
276 }
277 threadStarted = TRUE;
278
279 if (LsaConnectUntrusted(&lsa) != STATUS_SUCCESS)
280 {
281 (void)fprintf(stderr, "LsaConnectUntrusted failed\n");
282 goto cleanup;
283 }
284
285 char pkgName[] = "TSSSP";
286 LSA_STRING name = { (USHORT)strlen(pkgName), (USHORT)sizeof(pkgName), pkgName };
287 ULONG pkgId = 0;
288 if ((LsaLookupAuthenticationPackage(lsa, &name, &pkgId) != STATUS_SUCCESS) ||
289 (pkgId != FAKE_PACKAGE_ID))
290 {
291 (void)fprintf(stderr, "LsaLookupAuthenticationPackage failed (id=%lu)\n",
292 (unsigned long)pkgId);
293 goto cleanup;
294 }
295
296 BYTE submit[] = { 'r', 'd', 'p', 'e', 'a', 'r', 0x00, 0xFF };
297 PVOID ret = nullptr;
298 ULONG retLen = 0;
299 NTSTATUS protoStatus = 0;
300 if ((LsaCallAuthenticationPackage(lsa, pkgId, submit, sizeof(submit), &ret, &retLen,
301 &protoStatus) != STATUS_SUCCESS) ||
302 (retLen != sizeof(submit)) || !ret || (memcmp(ret, submit, sizeof(submit)) != 0))
303 {
304 (void)fprintf(stderr, "LsaCallAuthenticationPackage echo mismatch (len=%lu)\n",
305 (unsigned long)retLen);
306 (void)LsaFreeReturnBuffer(ret);
307 goto cleanup;
308 }
309 (void)LsaFreeReturnBuffer(ret);
310 rc = 0;
311
312cleanup:
313 if (lsa)
314 (void)LsaDeregisterLogonProcess(lsa);
315 if (listenFd >= 0)
316 close(listenFd);
317 if (threadStarted)
318 pthread_join(thread, nullptr);
319 if (sockPath)
320 (void)unlink(sockPath);
321 if (dirCreated)
322 (void)rmdir(dir);
323 free(sockPath);
324 free(dir);
325 return rc;
326}
327#endif
328
329int TestLsaClient(int argc, char* argv[])
330{
331 WINPR_UNUSED(argc);
332 WINPR_UNUSED(argv);
333
334 if (test_codec_roundtrip() != 0)
335 return -1;
336#if defined(__EMSCRIPTEN__)
337 /* No AF_UNIX sockets in emscripten, so the lsa4posix transport cannot be exercised. */
338 (void)fprintf(stderr, "skipping the lsa4posix transport test: no AF_UNIX sockets\n");
339#else
340 if (test_fake_daemon() != 0)
341 return -1;
342#endif
343 return 0;
344}