12#include <winpr/stream.h>
13#include <winpr/secapi.h>
15#include <winpr/environment.h>
16#include <winpr/path.h>
18#include "../lsa_proto.h"
20#define FAKE_PACKAGE_ID 0x2A
22#if !defined(__EMSCRIPTEN__)
27static BOOL rw_all(
int fd, BYTE* data,
size_t len, BOOL writing)
33 writing ? write(fd, &data[off], len - off) : read(fd, &data[off], len - off);
42WINPR_ATTR_MALLOC(Stream_Free, 1)
43static
wStream* frame_recv(
int fd)
45 BYTE pfx[4] = WINPR_C_ARRAY_INIT;
46 if (!rw_all(fd, pfx,
sizeof(pfx), FALSE))
49 wStream pbuffer = WINPR_C_ARRAY_INIT;
50 wStream* ps = Stream_StaticConstInit(&pbuffer, pfx,
sizeof(pfx));
52 Stream_Read_UINT32_BE(ps, len);
53 if ((len == 0) || (len > LSA_PROTO_MAX_FRAME))
55 wStream* s = Stream_New(
nullptr, len);
58 if (!rw_all(fd, Stream_Buffer(s), len, FALSE) || !Stream_SetLength(s, len) ||
59 !Stream_SetPosition(s, 0))
67static BOOL frame_send(
int fd,
wStream* payload)
69 const size_t len = Stream_Length(payload);
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);
80static void* fake_daemon(
void* arg)
82 const int listenFd = *(
const int*)arg;
83 const int conn = accept(listenFd,
nullptr,
nullptr);
95 if (!lsa_proto_reader_init(&r, in, &hdr))
97 Stream_Free(in, TRUE);
101 LSA_MSG_HDR rh = { hdr.type, LSA_MSG_FLAG_RESPONSE, hdr.requestId };
103 BOOL ok = lsa_proto_writer_init(&w, &rh);
105 if (ok && (hdr.type == LSA_MSG_HELLO))
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);
117 else if (ok && (hdr.type == LSA_MSG_LOOKUP_PACKAGE))
119 BYTE* name =
nullptr;
121 ok = lsa_proto_read_blob(&r, &name, &nameLen);
123 ok = ok && lsa_proto_write_u32(&w, STATUS_SUCCESS) &&
124 lsa_proto_write_u32(&w, FAKE_PACKAGE_ID);
126 else if (ok && (hdr.type == LSA_MSG_CALL_PACKAGE))
129 BYTE* submit =
nullptr;
130 UINT32 submitLen = 0;
131 ok = lsa_proto_read_u32(&r, &pkg) && lsa_proto_read_blob(&r, &submit, &submitLen);
133 ok = ok && lsa_proto_write_u32(&w, STATUS_SUCCESS) && lsa_proto_write_u32(&w, 0) &&
134 lsa_proto_write_blob(&w, submit, submitLen);
140 lsa_proto_reader_done(&r);
141 Stream_Free(in, TRUE);
144 if (ok && lsa_proto_writer_finish(&w, &out))
146 const BOOL sent = frame_send(conn, out);
147 Stream_Free(out, TRUE);
153 lsa_proto_writer_abort(&w);
163static int test_codec_roundtrip(
void)
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 };
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))
175 lsa_proto_writer_abort(&w);
176 (void)fprintf(stderr,
"codec: encode failed\n");
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;
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);
193 if ((total < 16) || (memcmp(common, commonHeader,
sizeof(common)) != 0) ||
194 (objectLength != total - 16) || ((objectLength % 8) != 0))
196 (void)fprintf(stderr,
"codec: not an NDR type-serialized object\n");
197 Stream_Free(framed, TRUE);
205 BYTE* blob =
nullptr;
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))
211 if ((got.type == LSA_MSG_CALL_PACKAGE) && (got.requestId == 0x12345678) && (pkg == 0x99) &&
212 (blobLen ==
sizeof(submit)) && (memcmp(blob, submit,
sizeof(submit)) == 0) &&
216 (
void)fprintf(stderr,
"codec: field mismatch\n");
219 (
void)fprintf(stderr,
"codec: decode failed\n");
221 lsa_proto_reader_done(&r);
222 Stream_Free(framed, TRUE);
226#if !defined(__EMSCRIPTEN__)
227static int test_fake_daemon(
void)
231 pthread_t thread = 0;
232 BOOL threadStarted = FALSE;
233 BOOL dirCreated = FALSE;
234 HANDLE lsa =
nullptr;
235 char* sockPath =
nullptr;
237 char* tmp = GetKnownPath(KNOWN_PATH_TEMP);
238 char* dir = tmp ? GetCombinedPath(tmp,
"lsatestXXXXXX") : nullptr;
240 if (!dir || !mkdtemp(dir))
242 (void)fprintf(stderr,
"creating the temporary socket directory failed\n");
247 struct sockaddr_un addr = WINPR_C_ARRAY_INIT;
248 sockPath = GetCombinedPath(dir,
"lsa.sock");
249 if (!sockPath || (strlen(sockPath) >=
sizeof(addr.sun_path)))
251 (void)fprintf(stderr,
"socket path too long for AF_UNIX\n");
255 listenFd = socket(AF_UNIX, SOCK_STREAM, 0);
258 (void)fprintf(stderr,
"socket(AF_UNIX) failed\n");
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))
266 (void)fprintf(stderr,
"bind/listen on %s failed\n", sockPath);
270 (void)SetEnvironmentVariableA(
"LSA4POSIX_SOCKET", sockPath);
272 if (pthread_create(&thread,
nullptr, fake_daemon, &listenFd) != 0)
274 (void)fprintf(stderr,
"pthread_create failed\n");
277 threadStarted = TRUE;
279 if (LsaConnectUntrusted(&lsa) != STATUS_SUCCESS)
281 (void)fprintf(stderr,
"LsaConnectUntrusted failed\n");
285 char pkgName[] =
"TSSSP";
286 LSA_STRING name = { (USHORT)strlen(pkgName), (USHORT)
sizeof(pkgName), pkgName };
288 if ((LsaLookupAuthenticationPackage(lsa, &name, &pkgId) != STATUS_SUCCESS) ||
289 (pkgId != FAKE_PACKAGE_ID))
291 (void)fprintf(stderr,
"LsaLookupAuthenticationPackage failed (id=%lu)\n",
292 (
unsigned long)pkgId);
296 BYTE submit[] = {
'r',
'd',
'p',
'e',
'a',
'r', 0x00, 0xFF };
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))
304 (void)fprintf(stderr,
"LsaCallAuthenticationPackage echo mismatch (len=%lu)\n",
305 (
unsigned long)retLen);
306 (void)LsaFreeReturnBuffer(ret);
309 (void)LsaFreeReturnBuffer(ret);
314 (void)LsaDeregisterLogonProcess(lsa);
318 pthread_join(thread,
nullptr);
320 (void)unlink(sockPath);
329int TestLsaClient(
int argc,
char* argv[])
334 if (test_codec_roundtrip() != 0)
336#if defined(__EMSCRIPTEN__)
338 (void)fprintf(stderr,
"skipping the lsa4posix transport test: no AF_UNIX sockets\n");
340 if (test_fake_daemon() != 0)