FreeRDP
Loading...
Searching...
No Matches
shadow_server.c
1
20
21#include <freerdp/config.h>
22
23#include <errno.h>
24#include <stdint.h>
25
26#include <winpr/assert.h>
27#include <winpr/crt.h>
28#include <winpr/ssl.h>
29#include <winpr/path.h>
30#include <winpr/cmdline.h>
31#include <winpr/winsock.h>
32
33#include <freerdp/log.h>
34#include <freerdp/version.h>
35
36#include <winpr/tools/makecert.h>
37
38#ifndef _WIN32
39#include <sys/select.h>
40#include <signal.h>
41#endif
42
43#include "shadow.h"
44#include "shadow_cliprdr.h"
45
46#define TAG SERVER_TAG("shadow")
47
48static const char bind_address[] = "bind-address,";
49
50#define fail_at(arg, rc) fail_at_((arg), (rc), __FILE__, __func__, __LINE__)
51static int fail_at_(const COMMAND_LINE_ARGUMENT_A* arg, int rc, const char* file, const char* fkt,
52 size_t line)
53{
54 const DWORD level = WLOG_ERROR;
55 wLog* log = WLog_Get(TAG);
56 if (WLog_IsLevelActive(log, level))
57 WLog_PrintTextMessage(log, level, line, file, fkt,
58 "Command line parsing failed at '%s' value '%s' [%d]", arg->Name,
59 arg->Value, rc);
60 return rc;
61}
62
63WINPR_ATTR_NODISCARD
64static int command_line_compare(const void* pa, const void* pb)
65{
66 const COMMAND_LINE_ARGUMENT_A* a = pa;
67 const COMMAND_LINE_ARGUMENT_A* b = pb;
68
69 if (!a && !b)
70 return 0;
71 if (!a)
72 return -1;
73 if (!b)
74 return 1;
75
76 return strcmp(a->Name, b->Name);
77}
78
79WINPR_ATTR_NODISCARD
80static int shadow_server_print_command_line_help(int argc, char** argv,
81 const COMMAND_LINE_ARGUMENT_A* largs)
82{
83 if ((argc < 1) || !largs || !argv)
84 return -1;
85
86 {
87 char* path = winpr_GetConfigFilePath(TRUE, "SAM");
88 printf("Usage: %s [options]\n", argv[0]);
89 printf("\n");
90 printf("Notes: By default NLA security is active.\n");
91 printf("\tIn this mode a SAM database is required.\n");
92 printf("\tProvide one with /sam-file:<file with path>\n");
93 printf("\telse the default path %s is used.\n", path);
94 printf("\tIf there is no existing SAM file authentication for all users will fail.\n");
95 printf("\n\tIf authentication against PAM is desired, start with -sec-nla (requires "
96 "compiled in "
97 "support for PAM)\n\n");
98 printf("Syntax:\n");
99 printf(" /flag (enables flag)\n");
100 printf(" /option:<value> (specifies option with value)\n");
101 printf(" +toggle -toggle (enables or disables toggle, where '/' is a synonym of '+')\n");
102 printf("\n");
103 free(path);
104 }
105
106 // TODO: Sort arguments
107 size_t nrArgs = 0;
108 {
109 const COMMAND_LINE_ARGUMENT_A* arg = largs;
110 while (arg->Name != nullptr)
111 {
112 nrArgs++;
113 arg++;
114 }
115 nrArgs++;
116 }
117 COMMAND_LINE_ARGUMENT_A* args_copy = calloc(nrArgs, sizeof(COMMAND_LINE_ARGUMENT_A));
118 if (!args_copy)
119 return -1;
120 memcpy(args_copy, largs, nrArgs * sizeof(COMMAND_LINE_ARGUMENT_A));
121 qsort(args_copy, nrArgs - 1, sizeof(COMMAND_LINE_ARGUMENT_A), command_line_compare);
122
123 const COMMAND_LINE_ARGUMENT_A* arg = args_copy;
124
125 int rc = -1;
126 do
127 {
128 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
129 {
130 printf(" %s", "/");
131 printf("%-20s\n", arg->Name);
132 printf("\t%s\n", arg->Text);
133 }
134 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
135 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
136 {
137 printf(" %s", "/");
138
139 if (arg->Format)
140 {
141 const size_t length = (strlen(arg->Name) + strlen(arg->Format) + 2);
142 char* str = (char*)calloc(length + 1, sizeof(char));
143
144 if (!str)
145 goto fail;
146
147 (void)sprintf_s(str, length + 1, "%s:%s", arg->Name, arg->Format);
148 (void)printf("%-20s\n", str);
149 free(str);
150 }
151 else
152 {
153 printf("%-20s\n", arg->Name);
154 }
155
156 printf("\t%s\n", arg->Text);
157 }
158 else if (arg->Flags & COMMAND_LINE_VALUE_BOOL)
159 {
160 const size_t length = strlen(arg->Name) + 32;
161 char* str = calloc(length + 1, sizeof(char));
162
163 if (!str)
164 goto fail;
165
166 (void)sprintf_s(str, length + 1, "%s (default:%s)", arg->Name,
167 arg->Default ? "on" : "off");
168 (void)printf(" %s", arg->Default ? "-" : "+");
169 (void)printf("%-20s\n", str);
170 (void)printf("\t%s\n", arg->Text);
171
172 free(str);
173 }
174 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
175
176 rc = 1;
177fail:
178 free(args_copy);
179 return rc;
180}
181
182int shadow_server_command_line_status_print(rdpShadowServer* server, int argc, char** argv,
183 int status, const COMMAND_LINE_ARGUMENT_A* cargs)
184{
185 WINPR_UNUSED(server);
186
187 if (status == COMMAND_LINE_STATUS_PRINT_VERSION)
188 {
189 printf("FreeRDP version %s (git %s)\n", FREERDP_VERSION_FULL, FREERDP_GIT_REVISION);
190 return COMMAND_LINE_STATUS_PRINT_VERSION;
191 }
192 else if (status == COMMAND_LINE_STATUS_PRINT_BUILDCONFIG)
193 {
194 printf("%s\n", freerdp_get_build_config());
195 return COMMAND_LINE_STATUS_PRINT_BUILDCONFIG;
196 }
197 else if (status == COMMAND_LINE_STATUS_PRINT)
198 {
199 return COMMAND_LINE_STATUS_PRINT;
200 }
201 else if (status < 0)
202 {
203 if (shadow_server_print_command_line_help(argc, argv, cargs) < 0)
204 return -1;
205
206 return COMMAND_LINE_STATUS_PRINT_HELP;
207 }
208
209 return 1;
210}
211
212/* A whole number from min to max, nothing else */
213WINPR_ATTR_NODISCARD
214static BOOL shadow_server_parse_u32(const char* text, UINT32 min, UINT32 max, UINT32* value)
215{
216 WINPR_ASSERT(value);
217 if (!text)
218 return FALSE;
219
220 char* end = nullptr;
221 errno = 0;
222 const unsigned long long parsed = strtoull(text, &end, 0);
223 if ((errno != 0) || !end || (*end != '\0') || (parsed < min) || (parsed > max))
224 return FALSE;
225 *value = (UINT32)parsed;
226 return TRUE;
227}
228
229/* The text after prefix, or nullptr when text does not start with it */
230WINPR_ATTR_NODISCARD
231static const char* shadow_server_after(const char* text, const char* prefix)
232{
233 const size_t length = strlen(prefix);
234 return (strncmp(text, prefix, length) == 0) ? &text[length] : nullptr;
235}
236
237/* A bit rate given in kbit/s (1 to 1000000), stored in the unit of its field: unit 1 for kbit/s,
238 * 1000 for bit/s */
239WINPR_ATTR_NODISCARD
240static BOOL shadow_server_parse_bitrate(const char* text, UINT32 unit, UINT32* value)
241{
242 UINT32 kbits = 0;
243 if (!shadow_server_parse_u32(text, 1, 1000000, &kbits))
244 return FALSE;
245 *value = kbits * unit;
246 return TRUE;
247}
248
249#if defined(WITH_GFX_AV1) || defined(WITH_GFX_AV2)
250/* vbr, cbr, cq or q: the order of FREERDP_AV1_RATECONTROL and FREERDP_AV2_RATECONTROL */
251WINPR_ATTR_NODISCARD
252static BOOL shadow_server_parse_av_mode(const char* text, UINT32* mode)
253{
254 const char* names[] = { "vbr", "cbr", "cq", "q" };
255 for (UINT32 x = 0; x < ARRAYSIZE(names); x++)
256 {
257 if (strcmp(text, names[x]) == 0)
258 {
259 *mode = x;
260 return TRUE;
261 }
262 }
263 return FALSE;
264}
265#endif
266
267/* /codec:<codec>:<key>:<value>[,<codec>:<key>:<value>...], for the codecs built in:
268 * h264: rate-control:vbr|cqp, bitrate:<kbit/s>, qp:<0-51>
269 * av1, av2: rate-control:vbr|cbr|cq|q, bitrate:<kbit/s>
270 * hevc: rate-control:vbr, bitrate:<kbit/s>
271 * Nothing changes unless the whole value is right. */
272WINPR_ATTR_NODISCARD
273static BOOL shadow_server_parse_codec(rdpShadowServer* server, const char* value)
274{
275 WINPR_ASSERT(server);
276
277 H264_RATECONTROL_MODE h264Mode = server->h264RateControlMode;
278 UINT32 h264BitRate = server->h264BitRate; /* bit/s */
279 UINT32 h264QP = server->h264QP;
280#if defined(WITH_GFX_AV1)
281 UINT32 av1Mode = (UINT32)server->AV1RateControlMode;
282 UINT32 av1BitRate = server->AV1BitRate; /* kbit/s */
283#endif
284#if defined(WITH_GFX_AV2)
285 UINT32 av2Mode = (UINT32)server->AV2RateControlMode;
286 UINT32 av2BitRate = server->AV2BitRate; /* kbit/s */
287#endif
288#if defined(WITH_GFX_AZURE)
289 FREERDP_HEVC_RATECONTROL hevcMode = server->HEVCRateControlMode;
290 UINT32 hevcBitRate = server->HEVCBitRate; /* bit/s: the H.264 context's */
291#endif
292
293 BOOL ok = (value != nullptr);
294 size_t count = 0;
295 char** args = ok ? CommandLineParseCommaSeparatedValues(value, &count) : nullptr;
296 if (ok && !args)
297 ok = FALSE;
298 for (size_t x = 0; ok && (x < count); x++)
299 {
300 const char* cur = args[x];
301 const char* rest = nullptr;
302 const char* v = nullptr;
303 if ((rest = shadow_server_after(cur, "h264:")) != nullptr)
304 {
305 if ((v = shadow_server_after(rest, "rate-control:")) != nullptr)
306 {
307 if (strcmp(v, "vbr") == 0)
308 h264Mode = H264_RATECONTROL_VBR;
309 else if (strcmp(v, "cqp") == 0)
310 h264Mode = H264_RATECONTROL_CQP;
311 else
312 ok = FALSE;
313 }
314 else if ((v = shadow_server_after(rest, "bitrate:")) != nullptr)
315 ok = shadow_server_parse_bitrate(v, 1000, &h264BitRate);
316 else if ((v = shadow_server_after(rest, "qp:")) != nullptr)
317 ok = shadow_server_parse_u32(v, 0, 51, &h264QP);
318 else
319 ok = FALSE;
320 }
321#if defined(WITH_GFX_AV1)
322 else if ((rest = shadow_server_after(cur, "av1:")) != nullptr)
323 {
324 if ((v = shadow_server_after(rest, "rate-control:")) != nullptr)
325 ok = shadow_server_parse_av_mode(v, &av1Mode);
326 else if ((v = shadow_server_after(rest, "bitrate:")) != nullptr)
327 ok = shadow_server_parse_bitrate(v, 1, &av1BitRate);
328 else
329 ok = FALSE;
330 }
331#endif
332#if defined(WITH_GFX_AV2)
333 else if ((rest = shadow_server_after(cur, "av2:")) != nullptr)
334 {
335 if ((v = shadow_server_after(rest, "rate-control:")) != nullptr)
336 ok = shadow_server_parse_av_mode(v, &av2Mode);
337 else if ((v = shadow_server_after(rest, "bitrate:")) != nullptr)
338 ok = shadow_server_parse_bitrate(v, 1, &av2BitRate);
339 else
340 ok = FALSE;
341 }
342#endif
343#if defined(WITH_GFX_AZURE)
344 else if ((rest = shadow_server_after(cur, "hevc:")) != nullptr)
345 {
346 /* only vbr: the HEVC encoder hands its mode to the H.264 context, which knows VBR
347 * and CQP only, and FREERDP_HEVC_CBR has CQP's value */
348 if ((v = shadow_server_after(rest, "rate-control:")) != nullptr)
349 {
350 if (strcmp(v, "vbr") == 0)
351 hevcMode = FREERDP_HEVC_VBR;
352 else
353 ok = FALSE;
354 }
355 else if ((v = shadow_server_after(rest, "bitrate:")) != nullptr)
356 ok = shadow_server_parse_bitrate(v, 1000, &hevcBitRate);
357 else
358 ok = FALSE;
359 }
360#endif
361 else
362 ok = FALSE; /* an unknown codec, or one not built in */
363 }
364 CommandLineParserFree(args);
365
366 if (!ok)
367 return FALSE;
368 server->h264RateControlMode = h264Mode;
369 server->h264BitRate = h264BitRate;
370 server->h264QP = h264QP;
371#if defined(WITH_GFX_AV1)
372 server->AV1RateControlMode = (FREERDP_AV1_RATECONTROL)av1Mode;
373 server->AV1BitRate = av1BitRate;
374#endif
375#if defined(WITH_GFX_AV2)
376 server->AV2RateControlMode = (FREERDP_AV2_RATECONTROL)av2Mode;
377 server->AV2BitRate = av2BitRate;
378#endif
379#if defined(WITH_GFX_AZURE)
380 server->HEVCRateControlMode = hevcMode;
381 server->HEVCBitRate = hevcBitRate;
382#endif
383 return TRUE;
384}
385
386int shadow_server_parse_command_line(rdpShadowServer* server, int argc, char** argv,
388{
389 int status = 0;
390 DWORD flags = 0;
391 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
392 rdpSettings* settings = server->settings;
393
394 if ((argc < 2) || !argv || !cargs)
395 return 1;
396
397 CommandLineClearArgumentsA(cargs);
398 flags = COMMAND_LINE_SEPARATOR_COLON;
399 flags |= COMMAND_LINE_SIGIL_SLASH | COMMAND_LINE_SIGIL_PLUS_MINUS;
400 status = CommandLineParseArgumentsA(argc, argv, cargs, flags, server, nullptr, nullptr);
401
402 if (status < 0)
403 return status;
404
405 arg = cargs;
406 errno = 0;
407
408 do
409 {
410 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
411 continue;
412
413 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "port")
414 {
415 long val = strtol(arg->Value, nullptr, 0);
416
417 if ((errno != 0) || (val <= 0) || (val > UINT16_MAX))
418 return fail_at(arg, COMMAND_LINE_ERROR);
419
420 server->port = (DWORD)val;
421 }
422 CommandLineSwitchCase(arg, "ipc-socket")
423 {
424 /* /bind-address is incompatible */
425 if (server->ipcSocket)
426 return fail_at(arg, COMMAND_LINE_ERROR);
427 server->ipcSocket = _strdup(arg->Value);
428
429 if (!server->ipcSocket)
430 return fail_at(arg, COMMAND_LINE_ERROR);
431 }
432 CommandLineSwitchCase(arg, "bind-address")
433 {
434 int rc = 0;
435 size_t len = strlen(arg->Value) + sizeof(bind_address);
436 /* /ipc-socket is incompatible */
437 if (server->ipcSocket)
438 return fail_at(arg, COMMAND_LINE_ERROR);
439 server->ipcSocket = calloc(len, sizeof(CHAR));
440
441 if (!server->ipcSocket)
442 return fail_at(arg, COMMAND_LINE_ERROR);
443
444 rc = _snprintf(server->ipcSocket, len, "%s%s", bind_address, arg->Value);
445 if ((rc < 0) || ((size_t)rc != len - 1))
446 return fail_at(arg, COMMAND_LINE_ERROR);
447 }
448 CommandLineSwitchCase(arg, "may-view")
449 {
450 server->mayView = arg->Value != nullptr;
451 }
452 CommandLineSwitchCase(arg, "bitmap-compat")
453 {
454 server->SupportMultiRectBitmapUpdates = arg->Value == nullptr;
455 }
456 CommandLineSwitchCase(arg, "may-interact")
457 {
458 server->mayInteract = arg->Value != nullptr;
459 }
460 CommandLineSwitchCase(arg, "server-side-cursor")
461 {
462 server->ShowMouseCursor = arg->Value != nullptr;
463 }
464 CommandLineSwitchCase(arg, "mouse-relative")
465 {
466 const BOOL val = arg->Value != nullptr;
467 if (!freerdp_settings_set_bool(settings, FreeRDP_MouseUseRelativeMove, val) ||
468 !freerdp_settings_set_bool(settings, FreeRDP_HasRelativeMouseEvent, val))
469 return fail_at(arg, COMMAND_LINE_ERROR);
470 }
471 CommandLineSwitchCase(arg, "max-connections")
472 {
473 errno = 0;
474 unsigned long val = strtoul(arg->Value, nullptr, 0);
475
476 if ((errno != 0) || (val > UINT32_MAX))
477 return fail_at(arg, COMMAND_LINE_ERROR);
478 server->maxClientsConnected = val;
479 }
480 CommandLineSwitchCase(arg, "rect")
481 {
482 char* p = nullptr;
483 char* tok[4];
484 long x = -1;
485 long y = -1;
486 long w = -1;
487 long h = -1;
488 char* str = _strdup(arg->Value);
489
490 if (!str)
491 return fail_at(arg, COMMAND_LINE_ERROR);
492
493 tok[0] = p = str;
494 p = strchr(p + 1, ',');
495
496 if (!p)
497 {
498 free(str);
499 return fail_at(arg, COMMAND_LINE_ERROR);
500 }
501
502 *p++ = '\0';
503 tok[1] = p;
504 p = strchr(p + 1, ',');
505
506 if (!p)
507 {
508 free(str);
509 return fail_at(arg, COMMAND_LINE_ERROR);
510 }
511
512 *p++ = '\0';
513 tok[2] = p;
514 p = strchr(p + 1, ',');
515
516 if (!p)
517 {
518 free(str);
519 return fail_at(arg, COMMAND_LINE_ERROR);
520 }
521
522 *p++ = '\0';
523 tok[3] = p;
524 x = strtol(tok[0], nullptr, 0);
525
526 if (errno != 0)
527 goto fail;
528
529 y = strtol(tok[1], nullptr, 0);
530
531 if (errno != 0)
532 goto fail;
533
534 w = strtol(tok[2], nullptr, 0);
535
536 if (errno != 0)
537 goto fail;
538
539 h = strtol(tok[3], nullptr, 0);
540
541 if (errno != 0)
542 goto fail;
543
544 fail:
545 free(str);
546
547 if ((x < 0) || (y < 0) || (w < 1) || (h < 1) || (errno != 0))
548 return fail_at(arg, COMMAND_LINE_ERROR);
549
550 if ((x > UINT16_MAX) || (y > UINT16_MAX) || (x + w > UINT16_MAX) ||
551 (y + h > UINT16_MAX))
552 return fail_at(arg, COMMAND_LINE_ERROR);
553 server->subRect.left = (UINT16)x;
554 server->subRect.top = (UINT16)y;
555 server->subRect.right = (UINT16)(x + w);
556 server->subRect.bottom = (UINT16)(y + h);
557 server->shareSubRect = TRUE;
558 }
559 CommandLineSwitchCase(arg, "auth")
560 {
561 server->authentication = arg->Value != nullptr;
562 }
563 CommandLineSwitchCase(arg, "remote-guard")
564 {
565 if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteCredentialGuard,
566 arg->Value != nullptr))
567 return fail_at(arg, COMMAND_LINE_ERROR);
568 }
569 CommandLineSwitchCase(arg, "restricted-admin")
570 {
571 if (!freerdp_settings_set_bool(settings, FreeRDP_RestrictedAdminModeSupported,
572 arg->Value != nullptr))
573 return fail_at(arg, COMMAND_LINE_ERROR);
574 }
575 CommandLineSwitchCase(arg, "vmconnect")
576 {
577 if (!freerdp_settings_set_bool(settings, FreeRDP_VmConnectMode, arg->Value != nullptr))
578 return fail_at(arg, COMMAND_LINE_ERROR);
579 }
580 CommandLineSwitchCase(arg, "sec")
581 {
582 if (strcmp("rdp", arg->Value) == 0) /* Standard RDP */
583 {
584 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, TRUE))
585 return fail_at(arg, COMMAND_LINE_ERROR);
586 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, FALSE))
587 return fail_at(arg, COMMAND_LINE_ERROR);
588 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, FALSE))
589 return fail_at(arg, COMMAND_LINE_ERROR);
590 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, FALSE))
591 return fail_at(arg, COMMAND_LINE_ERROR);
592 if (!freerdp_settings_set_bool(settings, FreeRDP_UseRdpSecurityLayer, TRUE))
593 return fail_at(arg, COMMAND_LINE_ERROR);
594 }
595 else if (strcmp("tls", arg->Value) == 0) /* TLS */
596 {
597 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, FALSE))
598 return fail_at(arg, COMMAND_LINE_ERROR);
599 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, TRUE))
600 return fail_at(arg, COMMAND_LINE_ERROR);
601 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, FALSE))
602 return fail_at(arg, COMMAND_LINE_ERROR);
603 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, FALSE))
604 return fail_at(arg, COMMAND_LINE_ERROR);
605 }
606 else if (strcmp("nla", arg->Value) == 0) /* NLA */
607 {
608 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, FALSE))
609 return fail_at(arg, COMMAND_LINE_ERROR);
610 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, FALSE))
611 return fail_at(arg, COMMAND_LINE_ERROR);
612 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, TRUE))
613 return fail_at(arg, COMMAND_LINE_ERROR);
614 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, FALSE))
615 return fail_at(arg, COMMAND_LINE_ERROR);
616 }
617 else if (strcmp("ext", arg->Value) == 0) /* NLA Extended */
618 {
619 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, FALSE))
620 return fail_at(arg, COMMAND_LINE_ERROR);
621 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, FALSE))
622 return fail_at(arg, COMMAND_LINE_ERROR);
623 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, FALSE))
624 return fail_at(arg, COMMAND_LINE_ERROR);
625 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, TRUE))
626 return fail_at(arg, COMMAND_LINE_ERROR);
627 }
628 else
629 {
630 WLog_ERR(TAG, "unknown protocol security: %s", arg->Value);
631 return fail_at(arg, COMMAND_LINE_ERROR_UNEXPECTED_VALUE);
632 }
633 }
634 CommandLineSwitchCase(arg, "sec-rdp")
635 {
636 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, arg->Value != nullptr))
637 return fail_at(arg, COMMAND_LINE_ERROR);
638 }
639 CommandLineSwitchCase(arg, "sec-tls")
640 {
641 if (!freerdp_settings_set_bool(settings, FreeRDP_TlsSecurity, arg->Value != nullptr))
642 return fail_at(arg, COMMAND_LINE_ERROR);
643 }
644 CommandLineSwitchCase(arg, "sec-nla")
645 {
646 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, arg->Value != nullptr))
647 return fail_at(arg, COMMAND_LINE_ERROR);
648 }
649 CommandLineSwitchCase(arg, "sec-ext")
650 {
651 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, arg->Value != nullptr))
652 return fail_at(arg, COMMAND_LINE_ERROR);
653 }
654 CommandLineSwitchCase(arg, "sam-file")
655 {
656 if (!freerdp_settings_set_string(settings, FreeRDP_NtlmSamFile, arg->Value))
657 return fail_at(arg, COMMAND_LINE_ERROR);
658 }
659 CommandLineSwitchCase(arg, "log-level")
660 {
661 wLog* root = WLog_GetRoot();
662
663 if (!WLog_SetStringLogLevel(root, arg->Value))
664 return fail_at(arg, COMMAND_LINE_ERROR);
665 }
666 CommandLineSwitchCase(arg, "log-filters")
667 {
668 if (!WLog_AddStringLogFilters(arg->Value))
669 return fail_at(arg, COMMAND_LINE_ERROR);
670 }
671 CommandLineSwitchCase(arg, "nsc")
672 {
673 if (!freerdp_settings_set_bool(settings, FreeRDP_NSCodec, arg->Value != nullptr))
674 return fail_at(arg, COMMAND_LINE_ERROR);
675 }
676 CommandLineSwitchCase(arg, "rfx")
677 {
678 if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteFxCodec, arg->Value != nullptr))
679 return fail_at(arg, COMMAND_LINE_ERROR);
680 }
681 CommandLineSwitchCase(arg, "gfx")
682 {
683 if (!freerdp_settings_set_bool(settings, FreeRDP_SupportGraphicsPipeline,
684 arg->Value != nullptr))
685 return fail_at(arg, COMMAND_LINE_ERROR);
686 }
687 CommandLineSwitchCase(arg, "codec")
688 {
689 if (!shadow_server_parse_codec(server, arg->Value))
690 return fail_at(arg, COMMAND_LINE_ERROR);
691 }
692#if defined(WITH_RDPEUDP_SERVER)
693 CommandLineSwitchCase(arg, "multitransport")
694 {
695 if ((arg->Value != nullptr) &&
696 !freerdp_settings_set_uint32(settings, FreeRDP_MultitransportFlags,
697 TRANSPORT_TYPE_UDP_FECR | SOFTSYNC_TCP_TO_UDP))
698 return fail_at(arg, COMMAND_LINE_ERROR);
699 }
700#endif
701 CommandLineSwitchCase(arg, "gfx-progressive")
702 {
703 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxProgressive, arg->Value != nullptr))
704 return fail_at(arg, COMMAND_LINE_ERROR);
705 }
706#if defined(WITH_GFX_AV1)
707 CommandLineSwitchCase(arg, "gfx-av1")
708 {
709 BOOL parseFailed = FALSE;
710 BOOL enabled = TRUE;
711 UINT32 profile = freerdp_settings_get_uint32(settings, FreeRDP_GfxCodecAV1Profile);
712 if (arg->Value)
713 {
714 size_t count = 0;
715 char** args = CommandLineParseCommaSeparatedValues(arg->Value, &count);
716 WINPR_ASSERT(args || (count == 0));
717 for (size_t x = 0; x < count; x++)
718 {
719 const char* cur = args[x];
720 if (strncmp("profile:", cur, 8) == 0)
721 {
722 const char* val = &cur[8];
723 if (strcmp("high", val) == 0)
724 profile = 1;
725 else if (strcmp("low", val) == 0)
726 profile = 0;
727 else
728 {
729 const unsigned long v = strtoul(val, nullptr, 0);
730 if ((errno != 0) || (v >= UINT32_MAX))
731 {
732 parseFailed = TRUE;
733 break;
734 }
735 else
736 profile = WINPR_ASSERTING_INT_CAST(UINT32, v);
737 }
738 }
739 else if (strcmp("off", cur) == 0)
740 {
741 enabled = FALSE;
742 }
743 else
744 {
745 parseFailed = TRUE;
746 break;
747 }
748 }
749 CommandLineParserFree(args);
750 }
751 if (parseFailed)
752 return fail_at(arg, COMMAND_LINE_ERROR);
753 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxCodecAV1, enabled))
754 return fail_at(arg, COMMAND_LINE_ERROR);
755 if (!freerdp_settings_set_uint32(settings, FreeRDP_GfxCodecAV1Profile, profile))
756 return fail_at(arg, COMMAND_LINE_ERROR);
757 }
758#endif
759#if defined(WITH_GFX_AV2)
760 CommandLineSwitchCase(arg, "gfx-av2")
761 {
762 BOOL parseFailed = FALSE;
763 BOOL enabled = TRUE;
764 UINT32 profile = freerdp_settings_get_uint32(settings, FreeRDP_GfxCodecAV2Profile);
765 if (arg->Value)
766 {
767 size_t count = 0;
768 char** args = CommandLineParseCommaSeparatedValues(arg->Value, &count);
769 WINPR_ASSERT(args || (count == 0));
770 for (size_t x = 0; x < count; x++)
771 {
772 const char* cur = args[x];
773 if (strncmp("profile:", cur, 8) == 0)
774 {
775 const char* val = &cur[8];
776 if (strcmp("high", val) == 0)
777 profile = 1;
778 else if (strcmp("low", val) == 0)
779 profile = 0;
780 else
781 {
782 const unsigned long v = strtoul(val, nullptr, 0);
783 if ((errno != 0) || (v >= UINT32_MAX))
784 {
785 parseFailed = TRUE;
786 break;
787 }
788 else
789 profile = WINPR_ASSERTING_INT_CAST(UINT32, v);
790 }
791 }
792 else if (strcmp("off", cur) == 0)
793 {
794 enabled = FALSE;
795 }
796 else
797 {
798 parseFailed = TRUE;
799 break;
800 }
801 }
802 CommandLineParserFree(args);
803 }
804 if (parseFailed)
805 return fail_at(arg, COMMAND_LINE_ERROR);
806 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxCodecAV2, enabled))
807 return fail_at(arg, COMMAND_LINE_ERROR);
808 if (!freerdp_settings_set_uint32(settings, FreeRDP_GfxCodecAV2Profile, profile))
809 return fail_at(arg, COMMAND_LINE_ERROR);
810 }
811#endif
812 CommandLineSwitchCase(arg, "gfx-rfx")
813 {
814 if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteFxCodec, arg->Value != nullptr))
815 return fail_at(arg, COMMAND_LINE_ERROR);
816 }
817 CommandLineSwitchCase(arg, "gfx-planar")
818 {
819 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxPlanar, arg->Value != nullptr))
820 return fail_at(arg, COMMAND_LINE_ERROR);
821 }
822 CommandLineSwitchCase(arg, "gfx-avc420")
823 {
824 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxH264, arg->Value != nullptr))
825 return fail_at(arg, COMMAND_LINE_ERROR);
826 }
827 CommandLineSwitchCase(arg, "gfx-avc444")
828 {
829 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxAVC444v2, arg->Value != nullptr))
830 return fail_at(arg, COMMAND_LINE_ERROR);
831 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxAVC444, arg->Value != nullptr))
832 return fail_at(arg, COMMAND_LINE_ERROR);
833 }
834#if defined(WITH_GFX_AZURE)
835 CommandLineSwitchCase(arg, "gfx-hevc")
836 {
837 BOOL parseFailed = FALSE;
838 BOOL enabled = TRUE;
839 BOOL enabled444 = TRUE;
840
841 if (arg->Value)
842 {
843 size_t count = 0;
844 char** args = CommandLineParseCommaSeparatedValues(arg->Value, &count);
845 WINPR_ASSERT(args || (count == 0));
846 for (size_t x = 0; x < count; x++)
847 {
848 const char* cur = args[x];
849 if (_stricmp("on", cur) == 0)
850 {
851 enabled = TRUE;
852 enabled444 = TRUE;
853 }
854 else if (_stricmp("i444", cur) == 0)
855 {
856 enabled = TRUE;
857 enabled444 = TRUE;
858 }
859 else if (_stricmp("i420", cur) == 0)
860 {
861 enabled = TRUE;
862 enabled444 = FALSE;
863 }
864 else if (_stricmp("off", cur) == 0)
865 {
866 enabled = FALSE;
867 enabled444 = FALSE;
868 }
869 else
870 {
871 parseFailed = TRUE;
872 break;
873 }
874 }
875 CommandLineParserFree(args);
876 }
877 if (parseFailed)
878 return fail_at(arg, COMMAND_LINE_ERROR);
879 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxCodecHEVC, enabled))
880 return fail_at(arg, COMMAND_LINE_ERROR);
881 if (!freerdp_settings_set_bool(settings, FreeRDP_GfxCodecHEVC444, enabled444))
882 return fail_at(arg, COMMAND_LINE_ERROR);
883 }
884#endif
885 CommandLineSwitchCase(arg, "keytab")
886 {
887 if (!freerdp_settings_set_string(settings, FreeRDP_KerberosKeytab, arg->Value))
888 return fail_at(arg, COMMAND_LINE_ERROR);
889 }
890 CommandLineSwitchCase(arg, "ccache")
891 {
892 if (!freerdp_settings_set_string(settings, FreeRDP_KerberosCache, arg->Value))
893 return fail_at(arg, COMMAND_LINE_ERROR);
894 }
895 CommandLineSwitchCase(arg, "tls-secrets-file")
896 {
897 if (!freerdp_settings_set_string(settings, FreeRDP_TlsSecretsFile, arg->Value))
898 return fail_at(arg, COMMAND_LINE_ERROR);
899 }
900 CommandLineSwitchDefault(arg)
901 {
902 }
903 CommandLineSwitchEnd(arg)
904 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
905
906 /* Before /monitors: the subsystem decides which monitors there are */
907 arg = CommandLineFindArgumentA(cargs, "subsystem");
908
909 if (arg && (arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
910 {
911 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
912 {
913 if (!shadow_subsystem_set_order(arg->Value))
914 return fail_at(arg, COMMAND_LINE_ERROR);
915 }
916 else
917 {
918 shadow_subsystem_list();
919 status = COMMAND_LINE_STATUS_PRINT;
920 }
921 }
922
923 arg = CommandLineFindArgumentA(cargs, "monitors");
924
925 if (arg && (arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
926 {
927 UINT32 numMonitors = 0;
928 MONITOR_DEF monitors[16] = WINPR_C_ARRAY_INIT;
929 numMonitors = shadow_enum_monitors(monitors, 16);
930
931 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
932 {
933 /* Select monitors. errno may hold anything from what ran before
934 * (enumerating the monitors makes system calls of its own) */
935 errno = 0;
936 long val = strtol(arg->Value, nullptr, 0);
937
938 if ((val < 0) || (errno != 0) || ((UINT32)val >= numMonitors))
939 {
940 WLog_ERR(TAG, "no monitor '%s': there are %" PRIu32 " (/monitors lists them)",
941 arg->Value, numMonitors);
942 status = COMMAND_LINE_STATUS_PRINT;
943 }
944
945 server->selectedMonitor = (UINT32)val;
946 }
947 else
948 {
949 /* List monitors */
950
951 for (UINT32 index = 0; index < numMonitors; index++)
952 {
953 const MONITOR_DEF* monitor = &monitors[index];
954 const INT64 width = monitor->right - monitor->left + 1;
955 const INT64 height = monitor->bottom - monitor->top + 1;
956 WLog_INFO(
957 TAG, " %s [%" PRIu32 "] %" PRId64 "x%" PRId64 "\t+%" PRId32 "+%" PRId32 "",
958 (monitor->flags == 1) ? "*" : " ", index, width, height, monitor->left,
959 monitor->top);
960 }
961
962 status = COMMAND_LINE_STATUS_PRINT;
963 }
964 }
965
966 /* If we want to disable authentication we need to ensure that NLA security
967 * is not activated. Only TLS and RDP security allow anonymous login.
968 */
969 if (!server->authentication && !freerdp_settings_get_bool(settings, FreeRDP_VmConnectMode))
970 {
971 if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity, FALSE))
972 return COMMAND_LINE_ERROR;
973 if (!freerdp_settings_set_bool(settings, FreeRDP_ExtSecurity, FALSE))
974 return COMMAND_LINE_ERROR;
975 }
976 return status;
977}
978
979WINPR_ATTR_NODISCARD
980static DWORD WINAPI shadow_server_thread(LPVOID arg)
981{
982 rdpShadowServer* server = (rdpShadowServer*)arg;
983 BOOL running = TRUE;
984 DWORD status = 0;
985 freerdp_listener* listener = server->listener;
986 if (shadow_subsystem_start(server->subsystem) < 0)
987 running = FALSE;
988
989 while (running)
990 {
991 HANDLE events[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
992 DWORD nCount = 0;
993 events[nCount++] = server->StopEvent;
994 nCount += listener->GetEventHandles(listener, &events[nCount], ARRAYSIZE(events) - nCount);
995
996 if (nCount <= 1)
997 {
998 WLog_ERR(TAG, "Failed to get FreeRDP file descriptor");
999 break;
1000 }
1001
1002 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1003
1004 switch (status)
1005 {
1006 case WAIT_FAILED:
1007 case WAIT_OBJECT_0:
1008 running = FALSE;
1009 break;
1010
1011 default:
1012 {
1013 if (!listener->CheckFileDescriptor(listener))
1014 {
1015 WLog_ERR(TAG, "Failed to check FreeRDP file descriptor");
1016 running = FALSE;
1017 }
1018 else
1019 {
1020#ifdef _WIN32
1021 Sleep(100); /* FIXME: listener event handles */
1022#endif
1023 }
1024 }
1025 break;
1026 }
1027 }
1028
1029 listener->Close(listener);
1030 shadow_subsystem_stop(server->subsystem);
1031
1032 /* Signal to the clients that server is being stopped and wait for them
1033 * to disconnect. */
1034 if (shadow_client_boardcast_quit(server, 0))
1035 {
1036 while (ArrayList_Count(server->clients) > 0)
1037 {
1038 Sleep(100);
1039 }
1040 }
1041
1042 ExitThread(0);
1043 return 0;
1044}
1045
1046WINPR_ATTR_NODISCARD
1047static BOOL open_port(rdpShadowServer* server, char* address)
1048{
1049 BOOL status = 0;
1050 char* modaddr = address;
1051
1052 if (modaddr)
1053 {
1054 if (modaddr[0] == '[')
1055 {
1056 char* end = strchr(address, ']');
1057 if (!end)
1058 {
1059 WLog_ERR(TAG, "Could not parse bind-address %s", address);
1060 return -1;
1061 }
1062 *end++ = '\0';
1063 if (strlen(end) > 0)
1064 {
1065 WLog_ERR(TAG, "Excess data after IPv6 address: '%s'", end);
1066 return -1;
1067 }
1068 modaddr++;
1069 }
1070 }
1071 status = server->listener->Open(server->listener, modaddr, (UINT16)server->port);
1072
1073 if (!status)
1074 {
1075 WLog_ERR(TAG,
1076 "Problem creating TCP listener. (Port already used or insufficient permissions?)");
1077 }
1078
1079 return status;
1080}
1081
1082int shadow_server_start(rdpShadowServer* server)
1083{
1084 BOOL ipc = 0;
1085 BOOL status = 0;
1086 WSADATA wsaData;
1087
1088 if (!server)
1089 return -1;
1090
1091 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
1092 return -1;
1093
1094#ifndef _WIN32
1095 (void)signal(SIGPIPE, SIG_IGN);
1096#endif
1097 server->screen = shadow_screen_new(server);
1098
1099 if (!server->screen)
1100 {
1101 WLog_ERR(TAG, "screen_new failed");
1102 return -1;
1103 }
1104
1105 server->capture = shadow_capture_new(server);
1106
1107 if (!server->capture)
1108 {
1109 WLog_ERR(TAG, "capture_new failed");
1110 return -1;
1111 }
1112
1113 /* Bind magic:
1114 *
1115 * empty ... bind TCP all
1116 * <local path> ... bind local (IPC)
1117 * bind-socket,<address> ... bind TCP to specified interface
1118 */
1119 ipc = server->ipcSocket && (strncmp(bind_address, server->ipcSocket,
1120 strnlen(bind_address, sizeof(bind_address))) != 0);
1121 if (!ipc)
1122 {
1123 size_t count = 0;
1124
1125 char** ptr = CommandLineParseCommaSeparatedValuesEx(nullptr, server->ipcSocket, &count);
1126 if (!ptr || (count <= 1))
1127 {
1128 if (server->ipcSocket == nullptr)
1129 {
1130 if (!open_port(server, nullptr))
1131 {
1132 CommandLineParserFree(ptr);
1133 return -1;
1134 }
1135 }
1136 else
1137 {
1138 CommandLineParserFree(ptr);
1139 return -1;
1140 }
1141 }
1142
1143 WINPR_ASSERT(ptr || (count == 0));
1144 for (size_t x = 1; x < count; x++)
1145 {
1146 BOOL success = open_port(server, ptr[x]);
1147 if (!success)
1148 {
1149 CommandLineParserFree(ptr);
1150 return -1;
1151 }
1152 }
1153 CommandLineParserFree(ptr);
1154 }
1155 else
1156 {
1157 status = server->listener->OpenLocal(server->listener, server->ipcSocket);
1158
1159 if (!status)
1160 {
1161 WLog_ERR(TAG, "Problem creating local socket listener. (Port already used or "
1162 "insufficient permissions?)");
1163 return -1;
1164 }
1165 }
1166
1167 if (!(server->thread =
1168 CreateThread(nullptr, 0, shadow_server_thread, (void*)server, 0, nullptr)))
1169 {
1170 return -1;
1171 }
1172
1173 return 0;
1174}
1175
1176int shadow_server_stop(rdpShadowServer* server)
1177{
1178 if (!server)
1179 return -1;
1180
1181 if (server->thread)
1182 {
1183 (void)SetEvent(server->StopEvent);
1184 (void)WaitForSingleObject(server->thread, INFINITE);
1185 (void)CloseHandle(server->thread);
1186 server->thread = nullptr;
1187 if (server->listener && server->listener->Close)
1188 server->listener->Close(server->listener);
1189 }
1190
1191 if (server->screen)
1192 {
1193 shadow_screen_free(server->screen);
1194 server->screen = nullptr;
1195 }
1196
1197 if (server->capture)
1198 {
1199 shadow_capture_free(server->capture);
1200 server->capture = nullptr;
1201 }
1202
1203 return 0;
1204}
1205
1206WINPR_ATTR_NODISCARD
1207static int shadow_server_init_config_path(rdpShadowServer* server)
1208{
1209 if (!server->ConfigPath)
1210 {
1211 char* configHome = freerdp_settings_get_config_path();
1212
1213 if (configHome)
1214 {
1215 if (!winpr_PathFileExists(configHome) && !winpr_PathMakePath(configHome, nullptr))
1216 {
1217 WLog_ERR(TAG, "Failed to create directory '%s'", configHome);
1218 free(configHome);
1219 return -1;
1220 }
1221
1222 server->ConfigPath = configHome;
1223 }
1224 }
1225
1226 if (!server->ConfigPath)
1227 return -1; /* no usable config path */
1228
1229 return 1;
1230}
1231
1232#if defined(WITH_WINPR_TOOLS)
1233WINPR_ATTR_NODISCARD
1234static BOOL shadow_server_create_certificate(rdpShadowServer* server, const char* filepath)
1235{
1236 BOOL rc = FALSE;
1237 char* makecert_argv[6] = { "makecert", "-rdp", "-live", "-silent", "-y", "5" };
1238
1239 WINPR_STATIC_ASSERT(ARRAYSIZE(makecert_argv) <= INT_MAX);
1240 const size_t makecert_argc = ARRAYSIZE(makecert_argv);
1241
1242 MAKECERT_CONTEXT* makecert = makecert_context_new();
1243
1244 if (!makecert)
1245 goto out_fail;
1246
1247 if (makecert_context_process(makecert, (int)makecert_argc, makecert_argv) < 0)
1248 goto out_fail;
1249
1250 if (makecert_context_set_output_file_name(makecert, "shadow") != 1)
1251 goto out_fail;
1252
1253 WINPR_ASSERT(server);
1254 WINPR_ASSERT(filepath);
1255 if (!winpr_PathFileExists(server->CertificateFile))
1256 {
1257 if (makecert_context_output_certificate_file(makecert, filepath) != 1)
1258 goto out_fail;
1259 }
1260
1261 if (!winpr_PathFileExists(server->PrivateKeyFile))
1262 {
1263 if (makecert_context_output_private_key_file(makecert, filepath) != 1)
1264 goto out_fail;
1265 }
1266 rc = TRUE;
1267out_fail:
1268 makecert_context_free(makecert);
1269 return rc;
1270}
1271#endif
1272
1273WINPR_ATTR_NODISCARD
1274static BOOL shadow_server_read_pem(rdpShadowServer* server)
1275{
1276 BOOL rc = FALSE;
1277 WINPR_ASSERT(server);
1278
1279 rdpSettings* settings = server->settings;
1280 WINPR_ASSERT(settings);
1281
1282 {
1283 rdpPrivateKey* key = freerdp_key_new_from_file_enc(server->PrivateKeyFile, nullptr);
1284 if (!key)
1285 {
1286 WLog_ERR(TAG, "Private Key PEM %s invalid, aborting", server->PrivateKeyFile);
1287 return FALSE;
1288 }
1289
1290 if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RdpServerRsaKey, key, 1))
1291 {
1292 WLog_ERR(TAG, "Private Key PEM %s valid, but some properties failed to be extracted",
1293 server->PrivateKeyFile);
1294 return FALSE;
1295 }
1296 }
1297 {
1298 rdpCertificate* cert = freerdp_certificate_new_from_file(server->CertificateFile);
1299 if (!cert)
1300 {
1301 WLog_ERR(TAG, "Certificate PEM %s invalid, aborting", server->CertificateFile);
1302 return FALSE;
1303 }
1304
1305 if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RdpServerCertificate, cert, 1))
1306 goto out_fail;
1307
1308 if (!freerdp_certificate_is_rdp_security_compatible(cert))
1309 {
1310 if (!freerdp_settings_set_bool(settings, FreeRDP_UseRdpSecurityLayer, FALSE))
1311 goto out_fail;
1312 if (!freerdp_settings_set_bool(settings, FreeRDP_RdpSecurity, FALSE))
1313 goto out_fail;
1314 }
1315 }
1316 rc = TRUE;
1317
1318out_fail:
1319 WLog_ERR(TAG, "Certificate PEM %s valid, but some properties failed to be extracted",
1320 server->CertificateFile);
1321 return rc;
1322}
1323
1324WINPR_ATTR_NODISCARD
1325static BOOL shadow_server_init_certificate(rdpShadowServer* server)
1326{
1327 char* filepath = nullptr;
1328 BOOL ret = FALSE;
1329
1330 WINPR_ASSERT(server);
1331
1332 if (!winpr_PathFileExists(server->ConfigPath) &&
1333 !winpr_PathMakePath(server->ConfigPath, nullptr))
1334 {
1335 WLog_ERR(TAG, "Failed to create directory '%s'", server->ConfigPath);
1336 return FALSE;
1337 }
1338
1339 if (!(filepath = GetCombinedPath(server->ConfigPath, "shadow")))
1340 return FALSE;
1341
1342 if (!winpr_PathFileExists(filepath) && !winpr_PathMakePath(filepath, nullptr))
1343 {
1344 if (!winpr_PathMakePath(filepath, nullptr))
1345 {
1346 WLog_ERR(TAG, "Failed to create directory '%s'", filepath);
1347 goto out_fail;
1348 }
1349 }
1350
1351 server->CertificateFile = GetCombinedPath(filepath, "shadow.crt");
1352 server->PrivateKeyFile = GetCombinedPath(filepath, "shadow.key");
1353
1354 if (!server->CertificateFile || !server->PrivateKeyFile)
1355 goto out_fail;
1356
1357 if ((!winpr_PathFileExists(server->CertificateFile)) ||
1358 (!winpr_PathFileExists(server->PrivateKeyFile)))
1359 {
1360#if defined(WITH_WINPR_TOOLS)
1361 if (!shadow_server_create_certificate(server, filepath))
1362#else
1363 const char* cexist = winpr_PathFileExists(server->CertificateFile) ? "exists" : "not found";
1364 const char* pexist = winpr_PathFileExists(server->PrivateKeyFile) ? "exists" : "not found";
1365 WLog_ERR(TAG, "PEM files do not exist: cert: %s (%s), key: %s (%s)",
1366 server->CertificateFile, cexist, server->PrivateKeyFile, pexist);
1367#endif
1368 goto out_fail;
1369 }
1370
1371 ret = shadow_server_read_pem(server);
1372
1373out_fail:
1374 free(filepath);
1375 return ret;
1376}
1377
1378WINPR_ATTR_NODISCARD
1379static BOOL shadow_server_check_peer_restrictions(freerdp_listener* listener)
1380{
1381 WINPR_ASSERT(listener);
1382
1383 rdpShadowServer* server = (rdpShadowServer*)listener->info;
1384 WINPR_ASSERT(server);
1385
1386 if (server->maxClientsConnected > 0)
1387 {
1388 const size_t count = ArrayList_Count(server->clients);
1389 if (count >= server->maxClientsConnected)
1390 {
1391 WLog_WARN(TAG, "connection limit [%" PRIuz "] reached, discarding client",
1392 server->maxClientsConnected);
1393 return FALSE;
1394 }
1395 }
1396 return TRUE;
1397}
1398
1399int shadow_server_init(rdpShadowServer* server)
1400{
1401 int status = 0;
1402 if (!winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT))
1403 return -1;
1404 if (!WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi()))
1405 return -1;
1406
1407 if (!(server->clients = ArrayList_New(TRUE)))
1408 goto fail;
1409
1410 if (!(server->StopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
1411 goto fail;
1412
1413 if (!InitializeCriticalSectionAndSpinCount(&(server->lock), 4000))
1414 goto fail;
1415
1416 status = shadow_server_init_config_path(server);
1417
1418 if (status < 0)
1419 goto fail;
1420
1421 if (!shadow_server_init_certificate(server))
1422 goto fail;
1423
1424 server->listener = freerdp_listener_new();
1425
1426 if (!server->listener)
1427 goto fail;
1428
1429 server->listener->info = (void*)server;
1430 server->listener->CheckPeerAcceptRestrictions = shadow_server_check_peer_restrictions;
1431 server->listener->PeerAccepted = shadow_client_accepted;
1432 server->clipboard = shadow_clipboard_new(server);
1433
1434 server->subsystem = shadow_subsystem_new();
1435
1436 if (!server->subsystem)
1437 goto fail;
1438
1439 status = shadow_subsystem_init(server->subsystem, server);
1440 if (status < 0)
1441 goto fail;
1442
1443 return status;
1444
1445fail:
1446 shadow_server_uninit(server);
1447 WLog_ERR(TAG, "Failed to initialize shadow server");
1448 return -1;
1449}
1450
1451int shadow_server_uninit(rdpShadowServer* server)
1452{
1453 if (!server)
1454 return -1;
1455
1456 shadow_server_stop(server);
1457 shadow_subsystem_uninit(server->subsystem);
1458 shadow_subsystem_free(server->subsystem);
1459 server->subsystem = nullptr;
1460 shadow_clipboard_free(server->clipboard);
1461 server->clipboard = nullptr;
1462 freerdp_listener_free(server->listener);
1463 server->listener = nullptr;
1464 free(server->CertificateFile);
1465 server->CertificateFile = nullptr;
1466 free(server->PrivateKeyFile);
1467 server->PrivateKeyFile = nullptr;
1468 free(server->ConfigPath);
1469 server->ConfigPath = nullptr;
1470 DeleteCriticalSection(&(server->lock));
1471 (void)CloseHandle(server->StopEvent);
1472 server->StopEvent = nullptr;
1473 ArrayList_Free(server->clients);
1474 server->clients = nullptr;
1475 return 1;
1476}
1477
1478rdpShadowServer* shadow_server_new(void)
1479{
1480 rdpShadowServer* server = nullptr;
1481 server = (rdpShadowServer*)calloc(1, sizeof(rdpShadowServer));
1482
1483 if (!server)
1484 return nullptr;
1485
1486 server->SupportMultiRectBitmapUpdates = TRUE;
1487 server->port = 3389;
1488 server->mayView = TRUE;
1489 server->mayInteract = TRUE;
1490 server->h264RateControlMode = H264_RATECONTROL_VBR;
1491 server->h264BitRate = 10000000;
1492 server->h264FrameRate = 30;
1493 server->h264QP = 0;
1494 server->h264HwAccel = FALSE;
1495 server->authentication = TRUE;
1496#if defined(WITH_GFX_AV1)
1497 server->AV1BitRate = 3000; /* kbit/s */
1498 server->AV1RateControlMode = FREERDP_AV1_VBR;
1499#endif
1500#if defined(WITH_GFX_AV2)
1501 server->AV2BitRate = 500;
1502 server->AV2RateControlMode = FREERDP_AV2_VBR;
1503#endif
1504#if defined(WITH_GFX_AZURE)
1505 server->HEVCBitRate = 500000;
1506 server->HEVCRateControlMode = FREERDP_HEVC_VBR;
1507#endif
1509 return server;
1510}
1511
1512void shadow_server_free(rdpShadowServer* server)
1513{
1514 if (!server)
1515 return;
1516
1517 free(server->ipcSocket);
1518 server->ipcSocket = nullptr;
1519 freerdp_settings_free(server->settings);
1520 server->settings = nullptr;
1521 free(server);
1522}
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API char * freerdp_settings_get_config_path(void)
return the configuration directory for the library
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
#define FREERDP_SETTINGS_SERVER_MODE
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.