21 #include <freerdp/config.h>
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>
33 #include <freerdp/log.h>
34 #include <freerdp/version.h>
36 #include <winpr/tools/makecert.h>
39 #include <sys/select.h>
45 #define TAG SERVER_TAG("shadow")
47 static const char bind_address[] =
"bind-address,";
49 #define fail_at(arg, rc) fail_at_((arg), (rc), __FILE__, __func__, __LINE__)
53 const DWORD level = WLOG_ERROR;
54 wLog* log = WLog_Get(TAG);
55 if (WLog_IsLevelActive(log, level))
56 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
57 "Command line parsing failed at '%s' value '%s' [%d]", arg->Name,
62 static int shadow_server_print_command_line_help(
int argc,
char** argv,
68 if ((argc < 1) || !largs || !argv)
71 char* path = winpr_GetConfigFilePath(TRUE,
"SAM");
72 printf(
"Usage: %s [options]\n", argv[0]);
74 printf(
"Notes: By default NLA security is active.\n");
75 printf(
"\tIn this mode a SAM database is required.\n");
76 printf(
"\tProvide one with /sam-file:<file with path>\n");
77 printf(
"\telse the default path %s is used.\n", path);
78 printf(
"\tIf there is no existing SAM file authentication for all users will fail.\n");
80 "\n\tIf authentication against PAM is desired, start with -sec-nla (requires compiled in "
81 "support for PAM)\n\n");
83 printf(
" /flag (enables flag)\n");
84 printf(
" /option:<value> (specifies option with value)\n");
85 printf(
" +toggle -toggle (enables or disables toggle, where '/' is a synonym of '+')\n");
93 if (arg->Flags & COMMAND_LINE_VALUE_FLAG)
96 printf(
"%-20s\n", arg->Name);
97 printf(
"\t%s\n", arg->Text);
99 else if ((arg->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
100 (arg->Flags & COMMAND_LINE_VALUE_OPTIONAL))
106 length = (strlen(arg->Name) + strlen(arg->Format) + 2);
107 str = (
char*)malloc(length + 1);
112 (void)sprintf_s(str, length + 1,
"%s:%s", arg->Name, arg->Format);
113 (void)printf(
"%-20s\n", str);
118 printf(
"%-20s\n", arg->Name);
121 printf(
"\t%s\n", arg->Text);
123 else if (arg->Flags & COMMAND_LINE_VALUE_BOOL)
125 length = strlen(arg->Name) + 32;
126 str = (
char*)malloc(length + 1);
131 (void)sprintf_s(str, length + 1,
"%s (default:%s)", arg->Name,
132 arg->Default ?
"on" :
"off");
133 (void)printf(
" %s", arg->Default ?
"-" :
"+");
134 (void)printf(
"%-20s\n", str);
136 (void)printf(
"\t%s\n", arg->Text);
138 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
143 int shadow_server_command_line_status_print(rdpShadowServer* server,
int argc,
char** argv,
146 WINPR_UNUSED(server);
148 if (status == COMMAND_LINE_STATUS_PRINT_VERSION)
150 printf(
"FreeRDP version %s (git %s)\n", FREERDP_VERSION_FULL, FREERDP_GIT_REVISION);
151 return COMMAND_LINE_STATUS_PRINT_VERSION;
153 else if (status == COMMAND_LINE_STATUS_PRINT_BUILDCONFIG)
155 printf(
"%s\n", freerdp_get_build_config());
156 return COMMAND_LINE_STATUS_PRINT_BUILDCONFIG;
158 else if (status == COMMAND_LINE_STATUS_PRINT)
160 return COMMAND_LINE_STATUS_PRINT;
164 if (shadow_server_print_command_line_help(argc, argv, cargs) < 0)
167 return COMMAND_LINE_STATUS_PRINT_HELP;
173 int shadow_server_parse_command_line(rdpShadowServer* server,
int argc,
char** argv,
179 rdpSettings* settings = server->settings;
181 if ((argc < 2) || !argv || !cargs)
184 CommandLineClearArgumentsA(cargs);
185 flags = COMMAND_LINE_SEPARATOR_COLON;
186 flags |= COMMAND_LINE_SIGIL_SLASH | COMMAND_LINE_SIGIL_PLUS_MINUS;
187 status = CommandLineParseArgumentsA(argc, argv, cargs, flags, server, NULL, NULL);
197 if (!(arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
200 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"port")
202 long val = strtol(arg->Value, NULL, 0);
204 if ((errno != 0) || (val <= 0) || (val > UINT16_MAX))
205 return fail_at(arg, COMMAND_LINE_ERROR);
207 server->port = (DWORD)val;
209 CommandLineSwitchCase(arg,
"ipc-socket")
212 if (server->ipcSocket)
213 return fail_at(arg, COMMAND_LINE_ERROR);
214 server->ipcSocket = _strdup(arg->Value);
216 if (!server->ipcSocket)
217 return fail_at(arg, COMMAND_LINE_ERROR);
219 CommandLineSwitchCase(arg,
"bind-address")
222 size_t len = strlen(arg->Value) +
sizeof(bind_address);
224 if (server->ipcSocket)
225 return fail_at(arg, COMMAND_LINE_ERROR);
226 server->ipcSocket = calloc(len,
sizeof(CHAR));
228 if (!server->ipcSocket)
229 return fail_at(arg, COMMAND_LINE_ERROR);
231 rc = _snprintf(server->ipcSocket, len,
"%s%s", bind_address, arg->Value);
232 if ((rc < 0) || ((
size_t)rc != len - 1))
233 return fail_at(arg, COMMAND_LINE_ERROR);
235 CommandLineSwitchCase(arg,
"may-view")
237 server->mayView = arg->Value ? TRUE : FALSE;
239 CommandLineSwitchCase(arg,
"may-interact")
241 server->mayInteract = arg->Value ? TRUE : FALSE;
243 CommandLineSwitchCase(arg,
"max-connections")
246 unsigned long val = strtoul(arg->Value, NULL, 0);
248 if ((errno != 0) || (val > UINT32_MAX))
249 return fail_at(arg, COMMAND_LINE_ERROR);
250 server->maxClientsConnected = val;
252 CommandLineSwitchCase(arg,
"rect")
260 char* str = _strdup(arg->Value);
263 return fail_at(arg, COMMAND_LINE_ERROR);
266 p = strchr(p + 1,
',');
271 return fail_at(arg, COMMAND_LINE_ERROR);
276 p = strchr(p + 1,
',');
281 return fail_at(arg, COMMAND_LINE_ERROR);
286 p = strchr(p + 1,
',');
291 return fail_at(arg, COMMAND_LINE_ERROR);
296 x = strtol(tok[0], NULL, 0);
301 y = strtol(tok[1], NULL, 0);
306 w = strtol(tok[2], NULL, 0);
311 h = strtol(tok[3], NULL, 0);
319 if ((x < 0) || (y < 0) || (w < 1) || (h < 1) || (errno != 0))
320 return fail_at(arg, COMMAND_LINE_ERROR);
322 if ((x > UINT16_MAX) || (y > UINT16_MAX) || (x + w > UINT16_MAX) ||
323 (y + h > UINT16_MAX))
324 return fail_at(arg, COMMAND_LINE_ERROR);
325 server->subRect.left = (UINT16)x;
326 server->subRect.top = (UINT16)y;
327 server->subRect.right = (UINT16)(x + w);
328 server->subRect.bottom = (UINT16)(y + h);
329 server->shareSubRect = TRUE;
331 CommandLineSwitchCase(arg,
"auth")
333 server->authentication = arg->Value ? TRUE : FALSE;
335 CommandLineSwitchCase(arg,
"remote-guard")
338 arg->Value ? TRUE : FALSE))
339 return fail_at(arg, COMMAND_LINE_ERROR);
341 CommandLineSwitchCase(arg,
"sec")
343 if (strcmp(
"rdp", arg->Value) == 0)
346 return fail_at(arg, COMMAND_LINE_ERROR);
348 return fail_at(arg, COMMAND_LINE_ERROR);
350 return fail_at(arg, COMMAND_LINE_ERROR);
352 return fail_at(arg, COMMAND_LINE_ERROR);
354 return fail_at(arg, COMMAND_LINE_ERROR);
356 else if (strcmp(
"tls", arg->Value) == 0)
359 return fail_at(arg, COMMAND_LINE_ERROR);
361 return fail_at(arg, COMMAND_LINE_ERROR);
363 return fail_at(arg, COMMAND_LINE_ERROR);
365 return fail_at(arg, COMMAND_LINE_ERROR);
367 else if (strcmp(
"nla", arg->Value) == 0)
370 return fail_at(arg, COMMAND_LINE_ERROR);
372 return fail_at(arg, COMMAND_LINE_ERROR);
374 return fail_at(arg, COMMAND_LINE_ERROR);
376 return fail_at(arg, COMMAND_LINE_ERROR);
378 else if (strcmp(
"ext", arg->Value) == 0)
381 return fail_at(arg, COMMAND_LINE_ERROR);
383 return fail_at(arg, COMMAND_LINE_ERROR);
385 return fail_at(arg, COMMAND_LINE_ERROR);
387 return fail_at(arg, COMMAND_LINE_ERROR);
391 WLog_ERR(TAG,
"unknown protocol security: %s", arg->Value);
392 return fail_at(arg, COMMAND_LINE_ERROR_UNEXPECTED_VALUE);
395 CommandLineSwitchCase(arg,
"sec-rdp")
398 arg->Value ? TRUE : FALSE))
399 return fail_at(arg, COMMAND_LINE_ERROR);
401 CommandLineSwitchCase(arg,
"sec-tls")
404 arg->Value ? TRUE : FALSE))
405 return fail_at(arg, COMMAND_LINE_ERROR);
407 CommandLineSwitchCase(arg,
"sec-nla")
410 arg->Value ? TRUE : FALSE))
411 return fail_at(arg, COMMAND_LINE_ERROR);
413 CommandLineSwitchCase(arg,
"sec-ext")
416 arg->Value ? TRUE : FALSE))
417 return fail_at(arg, COMMAND_LINE_ERROR);
419 CommandLineSwitchCase(arg,
"sam-file")
422 return fail_at(arg, COMMAND_LINE_ERROR);
424 CommandLineSwitchCase(arg,
"log-level")
426 wLog* root = WLog_GetRoot();
428 if (!WLog_SetStringLogLevel(root, arg->Value))
429 return fail_at(arg, COMMAND_LINE_ERROR);
431 CommandLineSwitchCase(arg,
"log-filters")
433 if (!WLog_AddStringLogFilters(arg->Value))
434 return fail_at(arg, COMMAND_LINE_ERROR);
436 CommandLineSwitchCase(arg,
"nsc")
439 return fail_at(arg, COMMAND_LINE_ERROR);
441 CommandLineSwitchCase(arg,
"rfx")
444 arg->Value ? TRUE : FALSE))
445 return fail_at(arg, COMMAND_LINE_ERROR);
447 CommandLineSwitchCase(arg,
"gfx")
450 arg->Value ? TRUE : FALSE))
451 return fail_at(arg, COMMAND_LINE_ERROR);
453 CommandLineSwitchCase(arg,
"gfx-progressive")
456 arg->Value ? TRUE : FALSE))
457 return fail_at(arg, COMMAND_LINE_ERROR);
459 CommandLineSwitchCase(arg,
"gfx-rfx")
462 arg->Value ? TRUE : FALSE))
463 return fail_at(arg, COMMAND_LINE_ERROR);
465 CommandLineSwitchCase(arg,
"gfx-planar")
468 return fail_at(arg, COMMAND_LINE_ERROR);
470 CommandLineSwitchCase(arg,
"gfx-avc420")
473 return fail_at(arg, COMMAND_LINE_ERROR);
475 CommandLineSwitchCase(arg,
"gfx-avc444")
478 arg->Value ? TRUE : FALSE))
479 return fail_at(arg, COMMAND_LINE_ERROR);
481 return fail_at(arg, COMMAND_LINE_ERROR);
483 CommandLineSwitchCase(arg,
"keytab")
486 return fail_at(arg, COMMAND_LINE_ERROR);
488 CommandLineSwitchCase(arg,
"ccache")
491 return fail_at(arg, COMMAND_LINE_ERROR);
493 CommandLineSwitchCase(arg,
"tls-secrets-file")
496 return fail_at(arg, COMMAND_LINE_ERROR);
498 CommandLineSwitchDefault(arg)
501 CommandLineSwitchEnd(arg)
502 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
504 arg = CommandLineFindArgumentA(cargs,
"monitors");
506 if (arg && (arg->Flags & COMMAND_LINE_ARGUMENT_PRESENT))
508 UINT32 numMonitors = 0;
510 numMonitors = shadow_enum_monitors(monitors, 16);
512 if (arg->Flags & COMMAND_LINE_VALUE_PRESENT)
515 long val = strtol(arg->Value, NULL, 0);
517 if ((val < 0) || (errno != 0) || ((UINT32)val >= numMonitors))
518 status = COMMAND_LINE_STATUS_PRINT;
520 server->selectedMonitor = (UINT32)val;
526 for (UINT32 index = 0; index < numMonitors; index++)
529 const INT64 width = monitor->right - monitor->left + 1;
530 const INT64 height = monitor->bottom - monitor->top + 1;
531 WLog_INFO(TAG,
" %s [%d] %" PRId64
"x%" PRId64
"\t+%" PRId32
"+%" PRId32
"",
532 (monitor->flags == 1) ?
"*" :
" ", index, width, height, monitor->left,
536 status = COMMAND_LINE_STATUS_PRINT;
543 if (!server->authentication)
546 return COMMAND_LINE_ERROR;
551 static DWORD WINAPI shadow_server_thread(LPVOID arg)
553 rdpShadowServer* server = (rdpShadowServer*)arg;
556 freerdp_listener* listener = server->listener;
557 shadow_subsystem_start(server->subsystem);
561 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
563 events[nCount++] = server->StopEvent;
564 nCount += listener->GetEventHandles(listener, &events[nCount], ARRAYSIZE(events) - nCount);
568 WLog_ERR(TAG,
"Failed to get FreeRDP file descriptor");
572 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
583 if (!listener->CheckFileDescriptor(listener))
585 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
599 listener->Close(listener);
600 shadow_subsystem_stop(server->subsystem);
604 if (shadow_client_boardcast_quit(server, 0))
606 while (ArrayList_Count(server->clients) > 0)
616 static BOOL open_port(rdpShadowServer* server,
char* address)
619 char* modaddr = address;
623 if (modaddr[0] ==
'[')
625 char* end = strchr(address,
']');
628 WLog_ERR(TAG,
"Could not parse bind-address %s", address);
634 WLog_ERR(TAG,
"Excess data after IPv6 address: '%s'", end);
640 status = server->listener->Open(server->listener, modaddr, (UINT16)server->port);
645 "Problem creating TCP listener. (Port already used or insufficient permissions?)");
651 int shadow_server_start(rdpShadowServer* server)
660 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
664 (void)signal(SIGPIPE, SIG_IGN);
666 server->screen = shadow_screen_new(server);
670 WLog_ERR(TAG,
"screen_new failed");
674 server->capture = shadow_capture_new(server);
676 if (!server->capture)
678 WLog_ERR(TAG,
"capture_new failed");
688 ipc = server->ipcSocket && (strncmp(bind_address, server->ipcSocket,
689 strnlen(bind_address,
sizeof(bind_address))) != 0);
694 char** ptr = CommandLineParseCommaSeparatedValuesEx(NULL, server->ipcSocket, &count);
695 if (!ptr || (count <= 1))
697 if (server->ipcSocket == NULL)
699 if (!open_port(server, NULL))
701 CommandLineParserFree(ptr);
707 CommandLineParserFree(ptr);
712 WINPR_ASSERT(ptr || (count == 0));
713 for (
size_t x = 1; x < count; x++)
715 BOOL success = open_port(server, ptr[x]);
718 CommandLineParserFree(ptr);
722 CommandLineParserFree(ptr);
726 status = server->listener->OpenLocal(server->listener, server->ipcSocket);
730 WLog_ERR(TAG,
"Problem creating local socket listener. (Port already used or "
731 "insufficient permissions?)");
736 if (!(server->thread = CreateThread(NULL, 0, shadow_server_thread, (
void*)server, 0, NULL)))
744 int shadow_server_stop(rdpShadowServer* server)
751 (void)SetEvent(server->StopEvent);
752 (void)WaitForSingleObject(server->thread, INFINITE);
753 (void)CloseHandle(server->thread);
754 server->thread = NULL;
755 if (server->listener && server->listener->Close)
756 server->listener->Close(server->listener);
761 shadow_screen_free(server->screen);
762 server->screen = NULL;
767 shadow_capture_free(server->capture);
768 server->capture = NULL;
774 static int shadow_server_init_config_path(rdpShadowServer* server)
776 if (!server->ConfigPath)
782 if (!winpr_PathFileExists(configHome) && !winpr_PathMakePath(configHome, 0))
784 WLog_ERR(TAG,
"Failed to create directory '%s'", configHome);
789 server->ConfigPath = configHome;
793 if (!server->ConfigPath)
799 static BOOL shadow_server_create_certificate(rdpShadowServer* server,
const char* filepath)
802 char* makecert_argv[6] = {
"makecert",
"-rdp",
"-live",
"-silent",
"-y",
"5" };
804 WINPR_STATIC_ASSERT(ARRAYSIZE(makecert_argv) <= INT_MAX);
805 const size_t makecert_argc = ARRAYSIZE(makecert_argv);
807 MAKECERT_CONTEXT* makecert = makecert_context_new();
812 if (makecert_context_process(makecert, (
int)makecert_argc, makecert_argv) < 0)
815 if (makecert_context_set_output_file_name(makecert,
"shadow") != 1)
818 WINPR_ASSERT(server);
819 WINPR_ASSERT(filepath);
820 if (!winpr_PathFileExists(server->CertificateFile))
822 if (makecert_context_output_certificate_file(makecert, filepath) != 1)
826 if (!winpr_PathFileExists(server->PrivateKeyFile))
828 if (makecert_context_output_private_key_file(makecert, filepath) != 1)
833 makecert_context_free(makecert);
836 static BOOL shadow_server_init_certificate(rdpShadowServer* server)
838 char* filepath = NULL;
841 WINPR_ASSERT(server);
843 if (!winpr_PathFileExists(server->ConfigPath) && !winpr_PathMakePath(server->ConfigPath, 0))
845 WLog_ERR(TAG,
"Failed to create directory '%s'", server->ConfigPath);
849 if (!(filepath = GetCombinedPath(server->ConfigPath,
"shadow")))
852 if (!winpr_PathFileExists(filepath) && !winpr_PathMakePath(filepath, 0))
854 if (!CreateDirectoryA(filepath, 0))
856 WLog_ERR(TAG,
"Failed to create directory '%s'", filepath);
861 server->CertificateFile = GetCombinedPath(filepath,
"shadow.crt");
862 server->PrivateKeyFile = GetCombinedPath(filepath,
"shadow.key");
864 if (!server->CertificateFile || !server->PrivateKeyFile)
867 if ((!winpr_PathFileExists(server->CertificateFile)) ||
868 (!winpr_PathFileExists(server->PrivateKeyFile)))
870 if (!shadow_server_create_certificate(server, filepath))
874 rdpSettings* settings = server->settings;
875 WINPR_ASSERT(settings);
877 rdpPrivateKey* key = freerdp_key_new_from_file(server->PrivateKeyFile);
883 rdpCertificate* cert = freerdp_certificate_new_from_file(server->CertificateFile);
890 if (!freerdp_certificate_is_rdp_security_compatible(cert))
903 static BOOL shadow_server_check_peer_restrictions(freerdp_listener* listener)
905 WINPR_ASSERT(listener);
907 rdpShadowServer* server = (rdpShadowServer*)listener->info;
908 WINPR_ASSERT(server);
910 if (server->maxClientsConnected > 0)
912 const size_t count = ArrayList_Count(server->clients);
913 if (count >= server->maxClientsConnected)
915 WLog_WARN(TAG,
"connection limit [%" PRIuz
"] reached, discarding client",
916 server->maxClientsConnected);
923 int shadow_server_init(rdpShadowServer* server)
926 winpr_InitializeSSL(WINPR_SSL_INIT_DEFAULT);
927 WTSRegisterWtsApiFunctionTable(FreeRDP_InitWtsApi());
929 if (!(server->clients = ArrayList_New(TRUE)))
932 if (!(server->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
935 if (!InitializeCriticalSectionAndSpinCount(&(server->lock), 4000))
938 status = shadow_server_init_config_path(server);
943 if (!shadow_server_init_certificate(server))
946 server->listener = freerdp_listener_new();
948 if (!server->listener)
951 server->listener->info = (
void*)server;
952 server->listener->CheckPeerAcceptRestrictions = shadow_server_check_peer_restrictions;
953 server->listener->PeerAccepted = shadow_client_accepted;
954 server->subsystem = shadow_subsystem_new();
956 if (!server->subsystem)
959 status = shadow_subsystem_init(server->subsystem, server);
966 shadow_server_uninit(server);
967 WLog_ERR(TAG,
"Failed to initialize shadow server");
971 int shadow_server_uninit(rdpShadowServer* server)
976 shadow_server_stop(server);
977 shadow_subsystem_uninit(server->subsystem);
978 shadow_subsystem_free(server->subsystem);
979 server->subsystem = NULL;
980 freerdp_listener_free(server->listener);
981 server->listener = NULL;
982 free(server->CertificateFile);
983 server->CertificateFile = NULL;
984 free(server->PrivateKeyFile);
985 server->PrivateKeyFile = NULL;
986 free(server->ConfigPath);
987 server->ConfigPath = NULL;
988 DeleteCriticalSection(&(server->lock));
989 (void)CloseHandle(server->StopEvent);
990 server->StopEvent = NULL;
991 ArrayList_Free(server->clients);
992 server->clients = NULL;
996 rdpShadowServer* shadow_server_new(
void)
998 rdpShadowServer* server = NULL;
999 server = (rdpShadowServer*)calloc(1,
sizeof(rdpShadowServer));
1004 server->port = 3389;
1005 server->mayView = TRUE;
1006 server->mayInteract = TRUE;
1007 server->h264RateControlMode = H264_RATECONTROL_VBR;
1008 server->h264BitRate = 10000000;
1009 server->h264FrameRate = 30;
1011 server->authentication = TRUE;
1016 void shadow_server_free(rdpShadowServer* server)
1021 free(server->ipcSocket);
1022 server->ipcSocket = NULL;
1024 server->settings = NULL;
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
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 char * freerdp_settings_get_config_path(void)
return the configuration directory for the library
FREERDP_API void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
#define FREERDP_SETTINGS_SERVER_MODE
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.