FreeRDP
client/common/file.c
1 
20 #include <freerdp/config.h>
21 
22 #include <errno.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 
26 #include <winpr/string.h>
27 #include <winpr/file.h>
28 #include <winpr/cast.h>
29 
30 #include <freerdp/client.h>
31 #include <freerdp/client/file.h>
32 #include <freerdp/client/cmdline.h>
33 
34 #include <freerdp/channels/urbdrc.h>
35 #include <freerdp/channels/rdpecam.h>
36 #include <freerdp/channels/location.h>
37 
48 #include <stdio.h>
49 #include <string.h>
50 
51 #include <winpr/wtypes.h>
52 #include <winpr/crt.h>
53 #include <winpr/path.h>
54 #include <freerdp/log.h>
55 #define TAG CLIENT_TAG("common")
56 
57 /*#define DEBUG_CLIENT_FILE 1*/
58 
59 static const BYTE BOM_UTF16_LE[2] = { 0xFF, 0xFE };
60 
61 // #define INVALID_INTEGER_VALUE 0xFFFFFFFF
62 
63 #define RDP_FILE_LINE_FLAG_FORMATTED 0x00000001
64 // #define RDP_FILE_LINE_FLAG_STANDARD 0x00000002
65 #define RDP_FILE_LINE_FLAG_TYPE_STRING 0x00000010
66 #define RDP_FILE_LINE_FLAG_TYPE_INTEGER 0x00000020
67 // #define RDP_FILE_LINE_FLAG_TYPE_BINARY 0x00000040
68 
69 struct rdp_file_line
70 {
71  char* name;
72  LPSTR sValue;
73  PBYTE bValue;
74 
75  size_t index;
76 
77  long iValue;
78  DWORD flags;
79  int valueLength;
80 };
81 typedef struct rdp_file_line rdpFileLine;
82 
83 struct rdp_file
84 {
85  DWORD UseMultiMon; /* use multimon */
86  LPSTR SelectedMonitors; /* selectedmonitors */
87  DWORD MaximizeToCurrentDisplays; /* maximizetocurrentdisplays */
88  DWORD SingleMonInWindowedMode; /* singlemoninwindowedmode */
89  DWORD ScreenModeId; /* screen mode id */
90  DWORD SpanMonitors; /* span monitors */
91  DWORD SmartSizing; /* smartsizing */
92  DWORD DynamicResolution; /* dynamic resolution */
93  DWORD EnableSuperSpan; /* enablesuperpan */
94  DWORD SuperSpanAccelerationFactor; /* superpanaccelerationfactor */
95 
96  DWORD DesktopWidth; /* desktopwidth */
97  DWORD DesktopHeight; /* desktopheight */
98  DWORD DesktopSizeId; /* desktop size id */
99  DWORD SessionBpp; /* session bpp */
100  DWORD DesktopScaleFactor; /* desktopscalefactor */
101 
102  DWORD Compression; /* compression */
103  DWORD KeyboardHook; /* keyboardhook */
104  DWORD DisableCtrlAltDel; /* disable ctrl+alt+del */
105 
106  DWORD AudioMode; /* audiomode */
107  DWORD AudioQualityMode; /* audioqualitymode */
108  DWORD AudioCaptureMode; /* audiocapturemode */
109  DWORD EncodeRedirectedVideoCapture; /* encode redirected video capture */
110  DWORD RedirectedVideoCaptureEncodingQuality; /* redirected video capture encoding quality */
111  DWORD VideoPlaybackMode; /* videoplaybackmode */
112 
113  DWORD ConnectionType; /* connection type */
114 
115  DWORD NetworkAutoDetect; /* networkautodetect */
116  DWORD BandwidthAutoDetect; /* bandwidthautodetect */
117 
118  DWORD PinConnectionBar; /* pinconnectionbar */
119  DWORD DisplayConnectionBar; /* displayconnectionbar */
120 
121  DWORD WorkspaceId; /* workspaceid */
122  DWORD EnableWorkspaceReconnect; /* enableworkspacereconnect */
123 
124  DWORD DisableWallpaper; /* disable wallpaper */
125  DWORD AllowFontSmoothing; /* allow font smoothing */
126  DWORD AllowDesktopComposition; /* allow desktop composition */
127  DWORD DisableFullWindowDrag; /* disable full window drag */
128  DWORD DisableMenuAnims; /* disable menu anims */
129  DWORD DisableThemes; /* disable themes */
130  DWORD DisableCursorSetting; /* disable cursor setting */
131 
132  DWORD BitmapCacheSize; /* bitmapcachesize */
133  DWORD BitmapCachePersistEnable; /* bitmapcachepersistenable */
134 
135  DWORD ServerPort; /* server port */
136 
137  LPSTR Username; /* username */
138  LPSTR Domain; /* domain */
139  LPSTR Password; /*password*/
140  PBYTE Password51; /* password 51 */
141 
142  LPSTR FullAddress; /* full address */
143  LPSTR AlternateFullAddress; /* alternate full address */
144 
145  LPSTR UsbDevicesToRedirect; /* usbdevicestoredirect */
146  DWORD RedirectDrives; /* redirectdrives */
147  DWORD RedirectPrinters; /* redirectprinters */
148  DWORD RedirectComPorts; /* redirectcomports */
149  DWORD RedirectLocation; /* redirectlocation */
150  DWORD RedirectSmartCards; /* redirectsmartcards */
151  DWORD RedirectWebauthN; /* redirectwebauthn */
152  LPSTR RedirectCameras; /* camerastoredirect */
153  DWORD RedirectClipboard; /* redirectclipboard */
154  DWORD RedirectPosDevices; /* redirectposdevices */
155  DWORD RedirectDirectX; /* redirectdirectx */
156  DWORD DisablePrinterRedirection; /* disableprinterredirection */
157  DWORD DisableClipboardRedirection; /* disableclipboardredirection */
158 
159  DWORD ConnectToConsole; /* connect to console */
160  DWORD AdministrativeSession; /* administrative session */
161  DWORD AutoReconnectionEnabled; /* autoreconnection enabled */
162  DWORD AutoReconnectMaxRetries; /* autoreconnect max retries */
163 
164  DWORD PublicMode; /* public mode */
165  DWORD AuthenticationLevel; /* authentication level */
166  DWORD PromptCredentialOnce; /* promptcredentialonce */
167  DWORD PromptForCredentials; /* prompt for credentials */
168  DWORD NegotiateSecurityLayer; /* negotiate security layer */
169  DWORD EnableCredSSPSupport; /* enablecredsspsupport */
170  DWORD EnableRdsAadAuth; /* enablerdsaadauth */
171 
172  DWORD RemoteApplicationMode; /* remoteapplicationmode */
173  LPSTR LoadBalanceInfo; /* loadbalanceinfo */
174 
175  LPSTR RemoteApplicationName; /* remoteapplicationname */
176  LPSTR RemoteApplicationIcon; /* remoteapplicationicon */
177  LPSTR RemoteApplicationProgram; /* remoteapplicationprogram */
178  LPSTR RemoteApplicationFile; /* remoteapplicationfile */
179  LPSTR RemoteApplicationGuid; /* remoteapplicationguid */
180  LPSTR RemoteApplicationCmdLine; /* remoteapplicationcmdline */
181  DWORD RemoteApplicationExpandCmdLine; /* remoteapplicationexpandcmdline */
182  DWORD RemoteApplicationExpandWorkingDir; /* remoteapplicationexpandworkingdir */
183  DWORD DisableConnectionSharing; /* disableconnectionsharing */
184  DWORD DisableRemoteAppCapsCheck; /* disableremoteappcapscheck */
185 
186  LPSTR AlternateShell; /* alternate shell */
187  LPSTR ShellWorkingDirectory; /* shell working directory */
188 
189  LPSTR GatewayHostname; /* gatewayhostname */
190  DWORD GatewayUsageMethod; /* gatewayusagemethod */
191  DWORD GatewayProfileUsageMethod; /* gatewayprofileusagemethod */
192  DWORD GatewayCredentialsSource; /* gatewaycredentialssource */
193 
194  LPSTR ResourceProvider; /* resourceprovider */
195 
196  LPSTR WvdEndpointPool; /* wvd endpoint pool */
197  LPSTR geo; /* geo */
198  LPSTR armpath; /* armpath */
199  LPSTR aadtenantid; /* aadtenantid" */
200  LPSTR diagnosticserviceurl; /* diagnosticserviceurl */
201  LPSTR hubdiscoverygeourl; /* hubdiscoverygeourl" */
202  LPSTR activityhint; /* activityhint */
203 
204  DWORD UseRedirectionServerName; /* use redirection server name */
205 
206  LPSTR GatewayAccessToken; /* gatewayaccesstoken */
207 
208  LPSTR DrivesToRedirect; /* drivestoredirect */
209  LPSTR DevicesToRedirect; /* devicestoredirect */
210  LPSTR WinPosStr; /* winposstr */
211 
212  LPSTR PreconnectionBlob; /* pcb */
213 
214  LPSTR KdcProxyName; /* kdcproxyname */
215  DWORD RdgIsKdcProxy; /* rdgiskdcproxy */
216 
217  DWORD align1;
218 
219  size_t lineCount;
220  size_t lineSize;
221  rdpFileLine* lines;
222 
223  ADDIN_ARGV* args;
224  void* context;
225 
226  DWORD flags;
227 };
228 
229 static const char key_str_username[] = "username";
230 static const char key_str_domain[] = "domain";
231 static const char key_str_password[] = "password";
232 static const char key_str_full_address[] = "full address";
233 static const char key_str_alternate_full_address[] = "alternate full address";
234 static const char key_str_usbdevicestoredirect[] = "usbdevicestoredirect";
235 static const char key_str_camerastoredirect[] = "camerastoredirect";
236 static const char key_str_loadbalanceinfo[] = "loadbalanceinfo";
237 static const char key_str_remoteapplicationname[] = "remoteapplicationname";
238 static const char key_str_remoteapplicationicon[] = "remoteapplicationicon";
239 static const char key_str_remoteapplicationprogram[] = "remoteapplicationprogram";
240 static const char key_str_remoteapplicationfile[] = "remoteapplicationfile";
241 static const char key_str_remoteapplicationguid[] = "remoteapplicationguid";
242 static const char key_str_remoteapplicationcmdline[] = "remoteapplicationcmdline";
243 static const char key_str_alternate_shell[] = "alternate shell";
244 static const char key_str_shell_working_directory[] = "shell working directory";
245 static const char key_str_gatewayhostname[] = "gatewayhostname";
246 static const char key_str_gatewayaccesstoken[] = "gatewayaccesstoken";
247 static const char key_str_resourceprovider[] = "resourceprovider";
248 static const char str_resourceprovider_arm[] = "arm";
249 static const char key_str_kdcproxyname[] = "kdcproxyname";
250 static const char key_str_drivestoredirect[] = "drivestoredirect";
251 static const char key_str_devicestoredirect[] = "devicestoredirect";
252 static const char key_str_winposstr[] = "winposstr";
253 static const char key_str_pcb[] = "pcb";
254 static const char key_str_selectedmonitors[] = "selectedmonitors";
255 
256 static const char key_str_wvd[] = "wvd endpoint pool";
257 static const char key_str_geo[] = "geo";
258 static const char key_str_armpath[] = "armpath";
259 static const char key_str_aadtenantid[] = "aadtenantid";
260 
261 static const char key_str_diagnosticserviceurl[] = "diagnosticserviceurl";
262 static const char key_str_hubdiscoverygeourl[] = "hubdiscoverygeourl";
263 
264 static const char key_str_activityhint[] = "activityhint";
265 
266 static const char key_int_rdgiskdcproxy[] = "rdgiskdcproxy";
267 static const char key_int_use_redirection_server_name[] = "use redirection server name";
268 static const char key_int_gatewaycredentialssource[] = "gatewaycredentialssource";
269 static const char key_int_gatewayprofileusagemethod[] = "gatewayprofileusagemethod";
270 static const char key_int_gatewayusagemethod[] = "gatewayusagemethod";
271 static const char key_int_disableremoteappcapscheck[] = "disableremoteappcapscheck";
272 static const char key_int_disableconnectionsharing[] = "disableconnectionsharing";
273 static const char key_int_remoteapplicationexpandworkingdir[] = "remoteapplicationexpandworkingdir";
274 static const char key_int_remoteapplicationexpandcmdline[] = "remoteapplicationexpandcmdline";
275 static const char key_int_remoteapplicationmode[] = "remoteapplicationmode";
276 static const char key_int_enablecredsspsupport[] = "enablecredsspsupport";
277 static const char key_int_enablerdsaadauth[] = "enablerdsaadauth";
278 static const char key_int_negotiate_security_layer[] = "negotiate security layer";
279 static const char key_int_prompt_for_credentials[] = "prompt for credentials";
280 static const char key_int_promptcredentialonce[] = "promptcredentialonce";
281 static const char key_int_authentication_level[] = "authentication level";
282 static const char key_int_public_mode[] = "public mode";
283 static const char key_int_autoreconnect_max_retries[] = "autoreconnect max retries";
284 static const char key_int_autoreconnection_enabled[] = "autoreconnection enabled";
285 static const char key_int_administrative_session[] = "administrative session";
286 static const char key_int_connect_to_console[] = "connect to console";
287 static const char key_int_disableclipboardredirection[] = "disableclipboardredirection";
288 static const char key_int_disableprinterredirection[] = "disableprinterredirection";
289 static const char key_int_redirectdirectx[] = "redirectdirectx";
290 static const char key_int_redirectposdevices[] = "redirectposdevices";
291 static const char key_int_redirectclipboard[] = "redirectclipboard";
292 static const char key_int_redirectsmartcards[] = "redirectsmartcards";
293 static const char key_int_redirectcomports[] = "redirectcomports";
294 static const char key_int_redirectlocation[] = "redirectlocation";
295 static const char key_int_redirectprinters[] = "redirectprinters";
296 static const char key_int_redirectdrives[] = "redirectdrives";
297 static const char key_int_server_port[] = "server port";
298 static const char key_int_bitmapcachepersistenable[] = "bitmapcachepersistenable";
299 static const char key_int_bitmapcachesize[] = "bitmapcachesize";
300 static const char key_int_disable_cursor_setting[] = "disable cursor setting";
301 static const char key_int_disable_themes[] = "disable themes";
302 static const char key_int_disable_menu_anims[] = "disable menu anims";
303 static const char key_int_disable_full_window_drag[] = "disable full window drag";
304 static const char key_int_allow_desktop_composition[] = "allow desktop composition";
305 static const char key_int_allow_font_smoothing[] = "allow font smoothing";
306 static const char key_int_disable_wallpaper[] = "disable wallpaper";
307 static const char key_int_enableworkspacereconnect[] = "enableworkspacereconnect";
308 static const char key_int_workspaceid[] = "workspaceid";
309 static const char key_int_displayconnectionbar[] = "displayconnectionbar";
310 static const char key_int_pinconnectionbar[] = "pinconnectionbar";
311 static const char key_int_bandwidthautodetect[] = "bandwidthautodetect";
312 static const char key_int_networkautodetect[] = "networkautodetect";
313 static const char key_int_connection_type[] = "connection type";
314 static const char key_int_videoplaybackmode[] = "videoplaybackmode";
315 static const char key_int_redirected_video_capture_encoding_quality[] =
316  "redirected video capture encoding quality";
317 static const char key_int_encode_redirected_video_capture[] = "encode redirected video capture";
318 static const char key_int_audiocapturemode[] = "audiocapturemode";
319 static const char key_int_audioqualitymode[] = "audioqualitymode";
320 static const char key_int_audiomode[] = "audiomode";
321 static const char key_int_disable_ctrl_alt_del[] = "disable ctrl+alt+del";
322 static const char key_int_keyboardhook[] = "keyboardhook";
323 static const char key_int_compression[] = "compression";
324 static const char key_int_desktopscalefactor[] = "desktopscalefactor";
325 static const char key_int_session_bpp[] = "session bpp";
326 static const char key_int_desktop_size_id[] = "desktop size id";
327 static const char key_int_desktopheight[] = "desktopheight";
328 static const char key_int_desktopwidth[] = "desktopwidth";
329 static const char key_int_superpanaccelerationfactor[] = "superpanaccelerationfactor";
330 static const char key_int_enablesuperpan[] = "enablesuperpan";
331 static const char key_int_dynamic_resolution[] = "dynamic resolution";
332 static const char key_int_smart_sizing[] = "smart sizing";
333 static const char key_int_span_monitors[] = "span monitors";
334 static const char key_int_screen_mode_id[] = "screen mode id";
335 static const char key_int_singlemoninwindowedmode[] = "singlemoninwindowedmode";
336 static const char key_int_maximizetocurrentdisplays[] = "maximizetocurrentdisplays";
337 static const char key_int_use_multimon[] = "use multimon";
338 static const char key_int_redirectwebauthn[] = "redirectwebauthn";
339 
340 static BOOL utils_str_is_empty(const char* str)
341 {
342  if (!str)
343  return TRUE;
344  if (strlen(str) == 0)
345  return TRUE;
346  return FALSE;
347 }
348 
349 static SSIZE_T freerdp_client_rdp_file_add_line(rdpFile* file);
350 static rdpFileLine* freerdp_client_rdp_file_find_line_by_name(const rdpFile* file,
351  const char* name);
352 static void freerdp_client_file_string_check_free(LPSTR str);
353 
354 static BOOL freerdp_client_rdp_file_find_integer_entry(rdpFile* file, const char* name,
355  DWORD** outValue, rdpFileLine** outLine)
356 {
357  WINPR_ASSERT(file);
358  WINPR_ASSERT(name);
359  WINPR_ASSERT(outValue);
360  WINPR_ASSERT(outLine);
361 
362  *outValue = NULL;
363  *outLine = NULL;
364 
365  if (_stricmp(name, key_int_use_multimon) == 0)
366  *outValue = &file->UseMultiMon;
367  else if (_stricmp(name, key_int_maximizetocurrentdisplays) == 0)
368  *outValue = &file->MaximizeToCurrentDisplays;
369  else if (_stricmp(name, key_int_singlemoninwindowedmode) == 0)
370  *outValue = &file->SingleMonInWindowedMode;
371  else if (_stricmp(name, key_int_screen_mode_id) == 0)
372  *outValue = &file->ScreenModeId;
373  else if (_stricmp(name, key_int_span_monitors) == 0)
374  *outValue = &file->SpanMonitors;
375  else if (_stricmp(name, key_int_smart_sizing) == 0)
376  *outValue = &file->SmartSizing;
377  else if (_stricmp(name, key_int_dynamic_resolution) == 0)
378  *outValue = &file->DynamicResolution;
379  else if (_stricmp(name, key_int_enablesuperpan) == 0)
380  *outValue = &file->EnableSuperSpan;
381  else if (_stricmp(name, key_int_superpanaccelerationfactor) == 0)
382  *outValue = &file->SuperSpanAccelerationFactor;
383  else if (_stricmp(name, key_int_desktopwidth) == 0)
384  *outValue = &file->DesktopWidth;
385  else if (_stricmp(name, key_int_desktopheight) == 0)
386  *outValue = &file->DesktopHeight;
387  else if (_stricmp(name, key_int_desktop_size_id) == 0)
388  *outValue = &file->DesktopSizeId;
389  else if (_stricmp(name, key_int_session_bpp) == 0)
390  *outValue = &file->SessionBpp;
391  else if (_stricmp(name, key_int_desktopscalefactor) == 0)
392  *outValue = &file->DesktopScaleFactor;
393  else if (_stricmp(name, key_int_compression) == 0)
394  *outValue = &file->Compression;
395  else if (_stricmp(name, key_int_keyboardhook) == 0)
396  *outValue = &file->KeyboardHook;
397  else if (_stricmp(name, key_int_disable_ctrl_alt_del) == 0)
398  *outValue = &file->DisableCtrlAltDel;
399  else if (_stricmp(name, key_int_audiomode) == 0)
400  *outValue = &file->AudioMode;
401  else if (_stricmp(name, key_int_audioqualitymode) == 0)
402  *outValue = &file->AudioQualityMode;
403  else if (_stricmp(name, key_int_audiocapturemode) == 0)
404  *outValue = &file->AudioCaptureMode;
405  else if (_stricmp(name, key_int_encode_redirected_video_capture) == 0)
406  *outValue = &file->EncodeRedirectedVideoCapture;
407  else if (_stricmp(name, key_int_redirected_video_capture_encoding_quality) == 0)
408  *outValue = &file->RedirectedVideoCaptureEncodingQuality;
409  else if (_stricmp(name, key_int_videoplaybackmode) == 0)
410  *outValue = &file->VideoPlaybackMode;
411  else if (_stricmp(name, key_int_connection_type) == 0)
412  *outValue = &file->ConnectionType;
413  else if (_stricmp(name, key_int_networkautodetect) == 0)
414  *outValue = &file->NetworkAutoDetect;
415  else if (_stricmp(name, key_int_bandwidthautodetect) == 0)
416  *outValue = &file->BandwidthAutoDetect;
417  else if (_stricmp(name, key_int_pinconnectionbar) == 0)
418  *outValue = &file->PinConnectionBar;
419  else if (_stricmp(name, key_int_displayconnectionbar) == 0)
420  *outValue = &file->DisplayConnectionBar;
421  else if (_stricmp(name, key_int_workspaceid) == 0)
422  *outValue = &file->WorkspaceId;
423  else if (_stricmp(name, key_int_enableworkspacereconnect) == 0)
424  *outValue = &file->EnableWorkspaceReconnect;
425  else if (_stricmp(name, key_int_disable_wallpaper) == 0)
426  *outValue = &file->DisableWallpaper;
427  else if (_stricmp(name, key_int_allow_font_smoothing) == 0)
428  *outValue = &file->AllowFontSmoothing;
429  else if (_stricmp(name, key_int_allow_desktop_composition) == 0)
430  *outValue = &file->AllowDesktopComposition;
431  else if (_stricmp(name, key_int_disable_full_window_drag) == 0)
432  *outValue = &file->DisableFullWindowDrag;
433  else if (_stricmp(name, key_int_disable_menu_anims) == 0)
434  *outValue = &file->DisableMenuAnims;
435  else if (_stricmp(name, key_int_disable_themes) == 0)
436  *outValue = &file->DisableThemes;
437  else if (_stricmp(name, key_int_disable_cursor_setting) == 0)
438  *outValue = &file->DisableCursorSetting;
439  else if (_stricmp(name, key_int_bitmapcachesize) == 0)
440  *outValue = &file->BitmapCacheSize;
441  else if (_stricmp(name, key_int_bitmapcachepersistenable) == 0)
442  *outValue = &file->BitmapCachePersistEnable;
443  else if (_stricmp(name, key_int_server_port) == 0)
444  *outValue = &file->ServerPort;
445  else if (_stricmp(name, key_int_redirectdrives) == 0)
446  *outValue = &file->RedirectDrives;
447  else if (_stricmp(name, key_int_redirectprinters) == 0)
448  *outValue = &file->RedirectPrinters;
449  else if (_stricmp(name, key_int_redirectcomports) == 0)
450  *outValue = &file->RedirectComPorts;
451  else if (_stricmp(name, key_int_redirectlocation) == 0)
452  *outValue = &file->RedirectLocation;
453  else if (_stricmp(name, key_int_redirectsmartcards) == 0)
454  *outValue = &file->RedirectSmartCards;
455  else if (_stricmp(name, key_int_redirectclipboard) == 0)
456  *outValue = &file->RedirectClipboard;
457  else if (_stricmp(name, key_int_redirectposdevices) == 0)
458  *outValue = &file->RedirectPosDevices;
459  else if (_stricmp(name, key_int_redirectdirectx) == 0)
460  *outValue = &file->RedirectDirectX;
461  else if (_stricmp(name, key_int_disableprinterredirection) == 0)
462  *outValue = &file->DisablePrinterRedirection;
463  else if (_stricmp(name, key_int_disableclipboardredirection) == 0)
464  *outValue = &file->DisableClipboardRedirection;
465  else if (_stricmp(name, key_int_connect_to_console) == 0)
466  *outValue = &file->ConnectToConsole;
467  else if (_stricmp(name, key_int_administrative_session) == 0)
468  *outValue = &file->AdministrativeSession;
469  else if (_stricmp(name, key_int_autoreconnection_enabled) == 0)
470  *outValue = &file->AutoReconnectionEnabled;
471  else if (_stricmp(name, key_int_autoreconnect_max_retries) == 0)
472  *outValue = &file->AutoReconnectMaxRetries;
473  else if (_stricmp(name, key_int_public_mode) == 0)
474  *outValue = &file->PublicMode;
475  else if (_stricmp(name, key_int_authentication_level) == 0)
476  *outValue = &file->AuthenticationLevel;
477  else if (_stricmp(name, key_int_promptcredentialonce) == 0)
478  *outValue = &file->PromptCredentialOnce;
479  else if ((_stricmp(name, key_int_prompt_for_credentials) == 0))
480  *outValue = &file->PromptForCredentials;
481  else if (_stricmp(name, key_int_negotiate_security_layer) == 0)
482  *outValue = &file->NegotiateSecurityLayer;
483  else if (_stricmp(name, key_int_enablecredsspsupport) == 0)
484  *outValue = &file->EnableCredSSPSupport;
485  else if (_stricmp(name, key_int_enablerdsaadauth) == 0)
486  *outValue = &file->EnableRdsAadAuth;
487  else if (_stricmp(name, key_int_remoteapplicationmode) == 0)
488  *outValue = &file->RemoteApplicationMode;
489  else if (_stricmp(name, key_int_remoteapplicationexpandcmdline) == 0)
490  *outValue = &file->RemoteApplicationExpandCmdLine;
491  else if (_stricmp(name, key_int_remoteapplicationexpandworkingdir) == 0)
492  *outValue = &file->RemoteApplicationExpandWorkingDir;
493  else if (_stricmp(name, key_int_disableconnectionsharing) == 0)
494  *outValue = &file->DisableConnectionSharing;
495  else if (_stricmp(name, key_int_disableremoteappcapscheck) == 0)
496  *outValue = &file->DisableRemoteAppCapsCheck;
497  else if (_stricmp(name, key_int_gatewayusagemethod) == 0)
498  *outValue = &file->GatewayUsageMethod;
499  else if (_stricmp(name, key_int_gatewayprofileusagemethod) == 0)
500  *outValue = &file->GatewayProfileUsageMethod;
501  else if (_stricmp(name, key_int_gatewaycredentialssource) == 0)
502  *outValue = &file->GatewayCredentialsSource;
503  else if (_stricmp(name, key_int_use_redirection_server_name) == 0)
504  *outValue = &file->UseRedirectionServerName;
505  else if (_stricmp(name, key_int_rdgiskdcproxy) == 0)
506  *outValue = &file->RdgIsKdcProxy;
507  else if (_stricmp(name, key_int_redirectwebauthn) == 0)
508  *outValue = &file->RedirectWebauthN;
509  else
510  {
511  rdpFileLine* line = freerdp_client_rdp_file_find_line_by_name(file, name);
512  if (!line)
513  return FALSE;
514  if (!(line->flags & RDP_FILE_LINE_FLAG_TYPE_INTEGER))
515  return FALSE;
516 
517  *outLine = line;
518  }
519 
520  return TRUE;
521 }
522 
523 static BOOL freerdp_client_rdp_file_find_string_entry(rdpFile* file, const char* name,
524  LPSTR** outValue, rdpFileLine** outLine)
525 {
526  WINPR_ASSERT(file);
527  WINPR_ASSERT(name);
528  WINPR_ASSERT(outValue);
529  WINPR_ASSERT(outLine);
530 
531  *outValue = NULL;
532  *outLine = NULL;
533 
534  if (_stricmp(name, key_str_username) == 0)
535  *outValue = &file->Username;
536  else if (_stricmp(name, key_str_domain) == 0)
537  *outValue = &file->Domain;
538  else if (_stricmp(name, key_str_password) == 0)
539  *outValue = &file->Password;
540  else if (_stricmp(name, key_str_full_address) == 0)
541  *outValue = &file->FullAddress;
542  else if (_stricmp(name, key_str_alternate_full_address) == 0)
543  *outValue = &file->AlternateFullAddress;
544  else if (_stricmp(name, key_str_usbdevicestoredirect) == 0)
545  *outValue = &file->UsbDevicesToRedirect;
546  else if (_stricmp(name, key_str_camerastoredirect) == 0)
547  *outValue = &file->RedirectCameras;
548  else if (_stricmp(name, key_str_loadbalanceinfo) == 0)
549  *outValue = &file->LoadBalanceInfo;
550  else if (_stricmp(name, key_str_remoteapplicationname) == 0)
551  *outValue = &file->RemoteApplicationName;
552  else if (_stricmp(name, key_str_remoteapplicationicon) == 0)
553  *outValue = &file->RemoteApplicationIcon;
554  else if (_stricmp(name, key_str_remoteapplicationprogram) == 0)
555  *outValue = &file->RemoteApplicationProgram;
556  else if (_stricmp(name, key_str_remoteapplicationfile) == 0)
557  *outValue = &file->RemoteApplicationFile;
558  else if (_stricmp(name, key_str_remoteapplicationguid) == 0)
559  *outValue = &file->RemoteApplicationGuid;
560  else if (_stricmp(name, key_str_remoteapplicationcmdline) == 0)
561  *outValue = &file->RemoteApplicationCmdLine;
562  else if (_stricmp(name, key_str_alternate_shell) == 0)
563  *outValue = &file->AlternateShell;
564  else if (_stricmp(name, key_str_shell_working_directory) == 0)
565  *outValue = &file->ShellWorkingDirectory;
566  else if (_stricmp(name, key_str_gatewayhostname) == 0)
567  *outValue = &file->GatewayHostname;
568  else if (_stricmp(name, key_str_resourceprovider) == 0)
569  *outValue = &file->ResourceProvider;
570  else if (_stricmp(name, key_str_wvd) == 0)
571  *outValue = &file->WvdEndpointPool;
572  else if (_stricmp(name, key_str_geo) == 0)
573  *outValue = &file->geo;
574  else if (_stricmp(name, key_str_armpath) == 0)
575  *outValue = &file->armpath;
576  else if (_stricmp(name, key_str_aadtenantid) == 0)
577  *outValue = &file->aadtenantid;
578  else if (_stricmp(name, key_str_diagnosticserviceurl) == 0)
579  *outValue = &file->diagnosticserviceurl;
580  else if (_stricmp(name, key_str_hubdiscoverygeourl) == 0)
581  *outValue = &file->hubdiscoverygeourl;
582  else if (_stricmp(name, key_str_activityhint) == 0)
583  *outValue = &file->activityhint;
584  else if (_stricmp(name, key_str_gatewayaccesstoken) == 0)
585  *outValue = &file->GatewayAccessToken;
586  else if (_stricmp(name, key_str_kdcproxyname) == 0)
587  *outValue = &file->KdcProxyName;
588  else if (_stricmp(name, key_str_drivestoredirect) == 0)
589  *outValue = &file->DrivesToRedirect;
590  else if (_stricmp(name, key_str_devicestoredirect) == 0)
591  *outValue = &file->DevicesToRedirect;
592  else if (_stricmp(name, key_str_winposstr) == 0)
593  *outValue = &file->WinPosStr;
594  else if (_stricmp(name, key_str_pcb) == 0)
595  *outValue = &file->PreconnectionBlob;
596  else if (_stricmp(name, key_str_selectedmonitors) == 0)
597  *outValue = &file->SelectedMonitors;
598  else
599  {
600  rdpFileLine* line = freerdp_client_rdp_file_find_line_by_name(file, name);
601  if (!line)
602  return FALSE;
603  if (!(line->flags & RDP_FILE_LINE_FLAG_TYPE_STRING))
604  return FALSE;
605 
606  *outLine = line;
607  }
608 
609  return TRUE;
610 }
611 
612 /*
613  * Set an integer in a rdpFile
614  *
615  * @return FALSE if a standard name was set, TRUE for a non-standard name, FALSE on error
616  *
617  */
618 static BOOL freerdp_client_rdp_file_set_integer(rdpFile* file, const char* name, long value)
619 {
620  DWORD* targetValue = NULL;
621  rdpFileLine* line = NULL;
622 #ifdef DEBUG_CLIENT_FILE
623  WLog_DBG(TAG, "%s:i:%ld", name, value);
624 #endif
625 
626  if (value < 0)
627  return FALSE;
628 
629  if (!freerdp_client_rdp_file_find_integer_entry(file, name, &targetValue, &line))
630  {
631  SSIZE_T index = freerdp_client_rdp_file_add_line(file);
632  if (index == -1)
633  return FALSE;
634  line = &file->lines[index];
635  }
636 
637  if (targetValue)
638  {
639  *targetValue = (DWORD)value;
640  return TRUE;
641  }
642 
643  if (line)
644  {
645  free(line->name);
646  line->name = _strdup(name);
647  if (!line->name)
648  {
649  free(line->name);
650  line->name = NULL;
651  return FALSE;
652  }
653 
654  line->iValue = value;
655  line->flags = RDP_FILE_LINE_FLAG_FORMATTED;
656  line->flags |= RDP_FILE_LINE_FLAG_TYPE_INTEGER;
657  line->valueLength = 0;
658  return TRUE;
659  }
660 
661  return FALSE;
662 }
663 
664 static BOOL freerdp_client_parse_rdp_file_integer(rdpFile* file, const char* name,
665  const char* value)
666 {
667  char* endptr = NULL;
668  long ivalue = 0;
669  errno = 0;
670  ivalue = strtol(value, &endptr, 0);
671 
672  if ((endptr == NULL) || (errno != 0) || (endptr == value) || (ivalue > INT32_MAX) ||
673  (ivalue < INT32_MIN))
674  {
675  if (file->flags & RDP_FILE_FLAG_PARSE_INT_RELAXED)
676  {
677  WLog_WARN(TAG, "Integer option %s has invalid value %s, using default", name, value);
678  return TRUE;
679  }
680  else
681  {
682  WLog_ERR(TAG, "Failed to convert RDP file integer option %s [value=%s]", name, value);
683  return FALSE;
684  }
685  }
686 
687  return freerdp_client_rdp_file_set_integer(file, name, ivalue);
688 }
689 
698 static BOOL freerdp_client_rdp_file_set_string(rdpFile* file, const char* name, const char* value)
699 {
700  LPSTR* targetValue = NULL;
701  rdpFileLine* line = NULL;
702 #ifdef DEBUG_CLIENT_FILE
703  WLog_DBG(TAG, "%s:s:%s", name, value);
704 #endif
705 
706  if (!name || !value)
707  return FALSE;
708 
709  if (!freerdp_client_rdp_file_find_string_entry(file, name, &targetValue, &line))
710  {
711  SSIZE_T index = freerdp_client_rdp_file_add_line(file);
712  if (index == -1)
713  return FALSE;
714  line = &file->lines[index];
715  }
716 
717  if (targetValue)
718  {
719  *targetValue = _strdup(value);
720  if (!(*targetValue))
721  return FALSE;
722  return TRUE;
723  }
724 
725  if (line)
726  {
727  free(line->name);
728  free(line->sValue);
729  line->name = _strdup(name);
730  line->sValue = _strdup(value);
731  if (!line->name || !line->sValue)
732  {
733  free(line->name);
734  free(line->sValue);
735  line->name = NULL;
736  line->sValue = NULL;
737  return FALSE;
738  }
739 
740  line->flags = RDP_FILE_LINE_FLAG_FORMATTED;
741  line->flags |= RDP_FILE_LINE_FLAG_TYPE_STRING;
742  line->valueLength = 0;
743  return TRUE;
744  }
745 
746  return FALSE;
747 }
748 
749 static BOOL freerdp_client_add_option(rdpFile* file, const char* option)
750 {
751  return freerdp_addin_argv_add_argument(file->args, option);
752 }
753 
754 static SSIZE_T freerdp_client_rdp_file_add_line(rdpFile* file)
755 {
756  SSIZE_T index = (SSIZE_T)file->lineCount;
757 
758  while ((file->lineCount + 1) > file->lineSize)
759  {
760  size_t new_size = 0;
761  rdpFileLine* new_line = NULL;
762  new_size = file->lineSize * 2;
763  new_line = (rdpFileLine*)realloc(file->lines, new_size * sizeof(rdpFileLine));
764 
765  if (!new_line)
766  return -1;
767 
768  file->lines = new_line;
769  file->lineSize = new_size;
770  }
771 
772  ZeroMemory(&(file->lines[file->lineCount]), sizeof(rdpFileLine));
773  file->lines[file->lineCount].index = (size_t)index;
774  (file->lineCount)++;
775  return index;
776 }
777 
778 static BOOL freerdp_client_parse_rdp_file_string(rdpFile* file, char* name, char* value)
779 {
780  return freerdp_client_rdp_file_set_string(file, name, value);
781 }
782 
783 static BOOL freerdp_client_parse_rdp_file_option(rdpFile* file, const char* option)
784 {
785  return freerdp_client_add_option(file, option);
786 }
787 
788 BOOL freerdp_client_parse_rdp_file_buffer(rdpFile* file, const BYTE* buffer, size_t size)
789 {
790  return freerdp_client_parse_rdp_file_buffer_ex(file, buffer, size, NULL);
791 }
792 
793 static BOOL trim(char** strptr)
794 {
795  char* start = NULL;
796  char* str = NULL;
797  char* end = NULL;
798 
799  start = str = *strptr;
800  if (!str)
801  return TRUE;
802  if (!(~((size_t)str)))
803  return TRUE;
804  end = str + strlen(str) - 1;
805 
806  while (isspace(*str))
807  str++;
808 
809  while ((end > str) && isspace(*end))
810  end--;
811  end[1] = '\0';
812  if (start == str)
813  *strptr = str;
814  else
815  {
816  *strptr = _strdup(str);
817  free(start);
818  return *strptr != NULL;
819  }
820 
821  return TRUE;
822 }
823 
824 static BOOL trim_strings(rdpFile* file)
825 {
826  if (!trim(&file->Username))
827  return FALSE;
828  if (!trim(&file->Domain))
829  return FALSE;
830  if (!trim(&file->AlternateFullAddress))
831  return FALSE;
832  if (!trim(&file->FullAddress))
833  return FALSE;
834  if (!trim(&file->UsbDevicesToRedirect))
835  return FALSE;
836  if (!trim(&file->RedirectCameras))
837  return FALSE;
838  if (!trim(&file->LoadBalanceInfo))
839  return FALSE;
840  if (!trim(&file->GatewayHostname))
841  return FALSE;
842  if (!trim(&file->GatewayAccessToken))
843  return FALSE;
844  if (!trim(&file->RemoteApplicationName))
845  return FALSE;
846  if (!trim(&file->RemoteApplicationIcon))
847  return FALSE;
848  if (!trim(&file->RemoteApplicationProgram))
849  return FALSE;
850  if (!trim(&file->RemoteApplicationFile))
851  return FALSE;
852  if (!trim(&file->RemoteApplicationGuid))
853  return FALSE;
854  if (!trim(&file->RemoteApplicationCmdLine))
855  return FALSE;
856  if (!trim(&file->AlternateShell))
857  return FALSE;
858  if (!trim(&file->ShellWorkingDirectory))
859  return FALSE;
860  if (!trim(&file->DrivesToRedirect))
861  return FALSE;
862  if (!trim(&file->DevicesToRedirect))
863  return FALSE;
864  if (!trim(&file->DevicesToRedirect))
865  return FALSE;
866  if (!trim(&file->WinPosStr))
867  return FALSE;
868  if (!trim(&file->PreconnectionBlob))
869  return FALSE;
870  if (!trim(&file->KdcProxyName))
871  return FALSE;
872  if (!trim(&file->SelectedMonitors))
873  return FALSE;
874 
875  for (size_t i = 0; i < file->lineCount; ++i)
876  {
877  rdpFileLine* curLine = &file->lines[i];
878  if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_STRING)
879  {
880  if (!trim(&curLine->sValue))
881  return FALSE;
882  }
883  }
884 
885  return TRUE;
886 }
887 
888 BOOL freerdp_client_parse_rdp_file_buffer_ex(rdpFile* file, const BYTE* buffer, size_t size,
889  rdp_file_fkt_parse parse)
890 {
891  BOOL rc = FALSE;
892  size_t length = 0;
893  char* line = NULL;
894  char* type = NULL;
895  char* context = NULL;
896  char* d1 = NULL;
897  char* d2 = NULL;
898  char* beg = NULL;
899  char* name = NULL;
900  char* value = NULL;
901  char* copy = NULL;
902 
903  if (!file)
904  return FALSE;
905  if (size < 2)
906  return FALSE;
907 
908  if ((buffer[0] == BOM_UTF16_LE[0]) && (buffer[1] == BOM_UTF16_LE[1]))
909  {
910  LPCWSTR uc = (LPCWSTR)(&buffer[2]);
911  size = size / sizeof(WCHAR) - 1;
912 
913  copy = ConvertWCharNToUtf8Alloc(uc, size, NULL);
914  if (!copy)
915  {
916  WLog_ERR(TAG, "Failed to convert RDP file from UCS2 to UTF8");
917  return FALSE;
918  }
919  }
920  else
921  {
922  copy = calloc(1, size + sizeof(BYTE));
923 
924  if (!copy)
925  return FALSE;
926 
927  memcpy(copy, buffer, size);
928  }
929 
930  line = strtok_s(copy, "\r\n", &context);
931 
932  while (line)
933  {
934  length = strnlen(line, size);
935 
936  if (length > 1)
937  {
938  beg = line;
939  if (beg[0] == '/')
940  {
941  if (!freerdp_client_parse_rdp_file_option(file, line))
942  goto fail;
943 
944  goto next_line; /* FreeRDP option */
945  }
946 
947  d1 = strchr(line, ':');
948 
949  if (!d1)
950  goto next_line; /* not first delimiter */
951 
952  type = &d1[1];
953  d2 = strchr(type, ':');
954 
955  if (!d2)
956  goto next_line; /* no second delimiter */
957 
958  if ((d2 - d1) != 2)
959  goto next_line; /* improper type length */
960 
961  *d1 = 0;
962  *d2 = 0;
963  name = beg;
964  value = &d2[1];
965 
966  if (parse && parse(file->context, name, *type, value))
967  {
968  }
969  else if (*type == 'i')
970  {
971  /* integer type */
972  if (!freerdp_client_parse_rdp_file_integer(file, name, value))
973  goto fail;
974  }
975  else if (*type == 's')
976  {
977  /* string type */
978  if (!freerdp_client_parse_rdp_file_string(file, name, value))
979  goto fail;
980  }
981  else if (*type == 'b')
982  {
983  /* binary type */
984  WLog_ERR(TAG, "Unsupported RDP file binary option %s [value=%s]", name, value);
985  }
986  }
987 
988  next_line:
989  line = strtok_s(NULL, "\r\n", &context);
990  }
991 
992  rc = trim_strings(file);
993 fail:
994  free(copy);
995  return rc;
996 }
997 
998 BOOL freerdp_client_parse_rdp_file(rdpFile* file, const char* name)
999 {
1000  return freerdp_client_parse_rdp_file_ex(file, name, NULL);
1001 }
1002 
1003 BOOL freerdp_client_parse_rdp_file_ex(rdpFile* file, const char* name, rdp_file_fkt_parse parse)
1004 {
1005  BOOL status = 0;
1006  BYTE* buffer = NULL;
1007  FILE* fp = NULL;
1008  size_t read_size = 0;
1009  INT64 file_size = 0;
1010  const char* fname = name;
1011 
1012  if (!file || !name)
1013  return FALSE;
1014 
1015  if (_strnicmp(fname, "file://", 7) == 0)
1016  fname = &name[7];
1017 
1018  fp = winpr_fopen(fname, "r");
1019  if (!fp)
1020  {
1021  WLog_ERR(TAG, "Failed to open RDP file %s", name);
1022  return FALSE;
1023  }
1024 
1025  (void)_fseeki64(fp, 0, SEEK_END);
1026  file_size = _ftelli64(fp);
1027  (void)_fseeki64(fp, 0, SEEK_SET);
1028 
1029  if (file_size < 1)
1030  {
1031  WLog_ERR(TAG, "RDP file %s is empty", name);
1032  (void)fclose(fp);
1033  return FALSE;
1034  }
1035 
1036  buffer = (BYTE*)malloc((size_t)file_size + 2);
1037 
1038  if (!buffer)
1039  {
1040  (void)fclose(fp);
1041  return FALSE;
1042  }
1043 
1044  read_size = fread(buffer, (size_t)file_size, 1, fp);
1045 
1046  if (!read_size)
1047  {
1048  if (!ferror(fp))
1049  read_size = (size_t)file_size;
1050  }
1051 
1052  (void)fclose(fp);
1053 
1054  if (read_size < 1)
1055  {
1056  WLog_ERR(TAG, "Could not read from RDP file %s", name);
1057  free(buffer);
1058  return FALSE;
1059  }
1060 
1061  buffer[file_size] = '\0';
1062  buffer[file_size + 1] = '\0';
1063  status = freerdp_client_parse_rdp_file_buffer_ex(file, buffer, (size_t)file_size, parse);
1064  free(buffer);
1065  return status;
1066 }
1067 
1068 static INLINE BOOL FILE_POPULATE_STRING(char** _target, const rdpSettings* _settings,
1069  FreeRDP_Settings_Keys_String _option)
1070 {
1071  WINPR_ASSERT(_target);
1072  WINPR_ASSERT(_settings);
1073 
1074  const char* str = freerdp_settings_get_string(_settings, _option);
1075  freerdp_client_file_string_check_free(*_target);
1076  *_target = (void*)~((size_t)NULL);
1077  if (str)
1078  {
1079  *_target = _strdup(str);
1080  if (!_target)
1081  return FALSE;
1082  }
1083  return TRUE;
1084 }
1085 
1086 static char* freerdp_client_channel_args_to_string(const rdpSettings* settings, const char* channel,
1087  const char* option)
1088 {
1089  ADDIN_ARGV* args = freerdp_dynamic_channel_collection_find(settings, channel);
1090  const char* filters[] = { option };
1091  if (!args || (args->argc < 2))
1092  return NULL;
1093 
1094  return CommandLineToCommaSeparatedValuesEx(args->argc - 1, args->argv + 1, filters,
1095  ARRAYSIZE(filters));
1096 }
1097 
1098 static BOOL rdp_opt_duplicate(const rdpSettings* _settings, FreeRDP_Settings_Keys_String _id,
1099  char** _key)
1100 {
1101  WINPR_ASSERT(_settings);
1102  WINPR_ASSERT(_key);
1103  const char* tmp = freerdp_settings_get_string(_settings, _id);
1104 
1105  if (tmp)
1106  {
1107  *_key = _strdup(tmp);
1108  if (!*_key)
1109  return FALSE;
1110  }
1111 
1112  return TRUE;
1113 }
1114 
1115 BOOL freerdp_client_populate_rdp_file_from_settings(rdpFile* file, const rdpSettings* settings)
1116 {
1117  FreeRDP_Settings_Keys_String index = FreeRDP_STRING_UNUSED;
1118  UINT32 LoadBalanceInfoLength = 0;
1119  const char* GatewayHostname = NULL;
1120  char* redirectCameras = NULL;
1121 
1122  if (!file || !settings)
1123  return FALSE;
1124 
1125  if (!FILE_POPULATE_STRING(&file->Domain, settings, FreeRDP_Domain) ||
1126  !FILE_POPULATE_STRING(&file->Username, settings, FreeRDP_Username) ||
1127  !FILE_POPULATE_STRING(&file->Password, settings, FreeRDP_Password) ||
1128  !FILE_POPULATE_STRING(&file->FullAddress, settings, FreeRDP_ServerHostname) ||
1129  !FILE_POPULATE_STRING(&file->AlternateFullAddress, settings, FreeRDP_ServerHostname) ||
1130  !FILE_POPULATE_STRING(&file->AlternateShell, settings, FreeRDP_AlternateShell) ||
1131  !FILE_POPULATE_STRING(&file->DrivesToRedirect, settings, FreeRDP_DrivesToRedirect))
1132 
1133  return FALSE;
1134  file->ServerPort = freerdp_settings_get_uint32(settings, FreeRDP_ServerPort);
1135 
1136  file->DesktopWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
1137  file->DesktopHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
1138  file->SessionBpp = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
1139  file->DesktopScaleFactor = freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
1140  file->DynamicResolution = WINPR_ASSERTING_INT_CAST(
1141  UINT32, freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate));
1142  file->VideoPlaybackMode = WINPR_ASSERTING_INT_CAST(
1143  UINT32, freerdp_settings_get_bool(settings, FreeRDP_SupportVideoOptimized));
1144 
1145  // TODO file->MaximizeToCurrentDisplays;
1146  // TODO file->SingleMonInWindowedMode;
1147  // TODO file->EncodeRedirectedVideoCapture;
1148  // TODO file->RedirectedVideoCaptureEncodingQuality;
1149  file->ConnectToConsole = WINPR_ASSERTING_INT_CAST(
1150  UINT32, freerdp_settings_get_bool(settings, FreeRDP_ConsoleSession));
1151  file->NegotiateSecurityLayer = WINPR_ASSERTING_INT_CAST(
1152  UINT32, freerdp_settings_get_bool(settings, FreeRDP_NegotiateSecurityLayer));
1153  file->EnableCredSSPSupport =
1154  WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_NlaSecurity));
1155  file->EnableRdsAadAuth =
1156  WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_AadSecurity));
1157 
1158  if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
1159  index = FreeRDP_RemoteApplicationWorkingDir;
1160  else
1161  index = FreeRDP_ShellWorkingDirectory;
1162  if (!FILE_POPULATE_STRING(&file->ShellWorkingDirectory, settings, index))
1163  return FALSE;
1164  file->ConnectionType = freerdp_settings_get_uint32(settings, FreeRDP_ConnectionType);
1165 
1166  file->ScreenModeId = freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) ? 2 : 1;
1167 
1168  LoadBalanceInfoLength = freerdp_settings_get_uint32(settings, FreeRDP_LoadBalanceInfoLength);
1169  if (LoadBalanceInfoLength > 0)
1170  {
1171  const BYTE* LoadBalanceInfo =
1172  freerdp_settings_get_pointer(settings, FreeRDP_LoadBalanceInfo);
1173  file->LoadBalanceInfo = calloc(LoadBalanceInfoLength + 1, 1);
1174  if (!file->LoadBalanceInfo)
1175  return FALSE;
1176  memcpy(file->LoadBalanceInfo, LoadBalanceInfo, LoadBalanceInfoLength);
1177  }
1178 
1179  if (freerdp_settings_get_bool(settings, FreeRDP_AudioPlayback))
1180  file->AudioMode = AUDIO_MODE_REDIRECT;
1181  else if (freerdp_settings_get_bool(settings, FreeRDP_RemoteConsoleAudio))
1182  file->AudioMode = AUDIO_MODE_PLAY_ON_SERVER;
1183  else
1184  file->AudioMode = AUDIO_MODE_NONE;
1185 
1186  /* The gateway hostname should also contain a port specifier unless it is the default port 443
1187  */
1188  GatewayHostname = freerdp_settings_get_string(settings, FreeRDP_GatewayHostname);
1189  if (GatewayHostname)
1190  {
1191  const UINT32 GatewayPort = freerdp_settings_get_uint32(settings, FreeRDP_GatewayPort);
1192  freerdp_client_file_string_check_free(file->GatewayHostname);
1193  if (GatewayPort == 443)
1194  file->GatewayHostname = _strdup(GatewayHostname);
1195  else
1196  {
1197  int length = _scprintf("%s:%" PRIu32, GatewayHostname, GatewayPort);
1198  if (length < 0)
1199  return FALSE;
1200 
1201  file->GatewayHostname = (char*)malloc((size_t)length + 1);
1202  if (!file->GatewayHostname)
1203  return FALSE;
1204 
1205  if (sprintf_s(file->GatewayHostname, (size_t)length + 1, "%s:%" PRIu32, GatewayHostname,
1206  GatewayPort) < 0)
1207  return FALSE;
1208  }
1209  if (!file->GatewayHostname)
1210  return FALSE;
1211  }
1212 
1213  if (freerdp_settings_get_bool(settings, FreeRDP_GatewayArmTransport))
1214  file->ResourceProvider = _strdup(str_resourceprovider_arm);
1215 
1216  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdWvdEndpointPool, &file->WvdEndpointPool))
1217  return FALSE;
1218  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdGeo, &file->geo))
1219  return FALSE;
1220  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdArmpath, &file->armpath))
1221  return FALSE;
1222  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdAadtenantid, &file->aadtenantid))
1223  return FALSE;
1224  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdDiagnosticserviceurl,
1225  &file->diagnosticserviceurl))
1226  return FALSE;
1227  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdHubdiscoverygeourl,
1228  &file->hubdiscoverygeourl))
1229  return FALSE;
1230  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdActivityhint, &file->activityhint))
1231  return FALSE;
1232 
1233  file->AudioCaptureMode =
1234  WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_AudioCapture));
1235  file->BitmapCachePersistEnable = WINPR_ASSERTING_INT_CAST(
1236  UINT32, freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled));
1237  file->Compression = WINPR_ASSERTING_INT_CAST(
1238  UINT32, freerdp_settings_get_bool(settings, FreeRDP_CompressionEnabled));
1239  file->AuthenticationLevel = freerdp_settings_get_uint32(settings, FreeRDP_AuthenticationLevel);
1240  file->GatewayUsageMethod = freerdp_settings_get_uint32(settings, FreeRDP_GatewayUsageMethod);
1241  file->GatewayCredentialsSource =
1242  freerdp_settings_get_uint32(settings, FreeRDP_GatewayCredentialsSource);
1243  file->PromptCredentialOnce = WINPR_ASSERTING_INT_CAST(
1244  UINT32, freerdp_settings_get_bool(settings, FreeRDP_GatewayUseSameCredentials));
1245  file->PromptForCredentials = WINPR_ASSERTING_INT_CAST(
1246  UINT32, freerdp_settings_get_bool(settings, FreeRDP_PromptForCredentials));
1247  file->RemoteApplicationMode = WINPR_ASSERTING_INT_CAST(
1248  UINT32, freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode));
1249  if (!FILE_POPULATE_STRING(&file->GatewayAccessToken, settings, FreeRDP_GatewayAccessToken) ||
1250  !FILE_POPULATE_STRING(&file->RemoteApplicationProgram, settings,
1251  FreeRDP_RemoteApplicationProgram) ||
1252  !FILE_POPULATE_STRING(&file->RemoteApplicationName, settings,
1253  FreeRDP_RemoteApplicationName) ||
1254  !FILE_POPULATE_STRING(&file->RemoteApplicationIcon, settings,
1255  FreeRDP_RemoteApplicationIcon) ||
1256  !FILE_POPULATE_STRING(&file->RemoteApplicationFile, settings,
1257  FreeRDP_RemoteApplicationFile) ||
1258  !FILE_POPULATE_STRING(&file->RemoteApplicationGuid, settings,
1259  FreeRDP_RemoteApplicationGuid) ||
1260  !FILE_POPULATE_STRING(&file->RemoteApplicationCmdLine, settings,
1261  FreeRDP_RemoteApplicationCmdLine))
1262  return FALSE;
1263  file->SpanMonitors =
1264  WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors));
1265  file->UseMultiMon =
1266  WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_UseMultimon));
1267  file->AllowDesktopComposition = WINPR_ASSERTING_INT_CAST(
1268  UINT32, freerdp_settings_get_bool(settings, FreeRDP_AllowDesktopComposition));
1269  file->AllowFontSmoothing = WINPR_ASSERTING_INT_CAST(
1270  UINT32, freerdp_settings_get_bool(settings, FreeRDP_AllowFontSmoothing));
1271  file->DisableWallpaper = WINPR_ASSERTING_INT_CAST(
1272  UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableWallpaper));
1273  file->DisableFullWindowDrag = WINPR_ASSERTING_INT_CAST(
1274  UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableFullWindowDrag));
1275  file->DisableMenuAnims = WINPR_ASSERTING_INT_CAST(
1276  UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableMenuAnims));
1277  file->DisableThemes = WINPR_ASSERTING_INT_CAST(
1278  UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableThemes));
1279  file->BandwidthAutoDetect = (freerdp_settings_get_uint32(settings, FreeRDP_ConnectionType) >=
1280  CONNECTION_TYPE_AUTODETECT)
1281  ? TRUE
1282  : FALSE;
1283  file->NetworkAutoDetect =
1284  freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect) ? 1 : 0;
1285  file->AutoReconnectionEnabled = WINPR_ASSERTING_INT_CAST(
1286  UINT32, freerdp_settings_get_bool(settings, FreeRDP_AutoReconnectionEnabled));
1287  file->RedirectSmartCards = WINPR_ASSERTING_INT_CAST(
1288  UINT32, freerdp_settings_get_bool(settings, FreeRDP_RedirectSmartCards));
1289  file->RedirectWebauthN = WINPR_ASSERTING_INT_CAST(
1290  UINT32, freerdp_settings_get_bool(settings, FreeRDP_RedirectWebAuthN));
1291 
1292  redirectCameras =
1293  freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "device:");
1294  if (redirectCameras)
1295  {
1296  char* str =
1297  freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "encode:");
1298  file->EncodeRedirectedVideoCapture = 0;
1299  if (str)
1300  {
1301  unsigned long val = 0;
1302  errno = 0;
1303  val = strtoul(str, NULL, 0);
1304  if ((val < UINT32_MAX) && (errno == 0))
1305  file->EncodeRedirectedVideoCapture = (UINT32)val;
1306  }
1307  free(str);
1308 
1309  str = freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "quality:");
1310  file->RedirectedVideoCaptureEncodingQuality = 0;
1311  if (str)
1312  {
1313  unsigned long val = 0;
1314  errno = 0;
1315  val = strtoul(str, NULL, 0);
1316  if ((val <= 2) && (errno == 0))
1317  {
1318  file->RedirectedVideoCaptureEncodingQuality = (UINT32)val;
1319  }
1320  }
1321  free(str);
1322 
1323  file->RedirectCameras = redirectCameras;
1324  }
1325 #ifdef CHANNEL_URBDRC_CLIENT
1326  char* redirectUsb =
1327  freerdp_client_channel_args_to_string(settings, URBDRC_CHANNEL_NAME, "device:");
1328  if (redirectUsb)
1329  file->UsbDevicesToRedirect = redirectUsb;
1330 
1331 #endif
1332  file->RedirectClipboard =
1333  freerdp_settings_get_bool(settings, FreeRDP_RedirectClipboard) ? 1 : 0;
1334  file->RedirectPrinters = freerdp_settings_get_bool(settings, FreeRDP_RedirectPrinters) ? 1 : 0;
1335  file->RedirectDrives = freerdp_settings_get_bool(settings, FreeRDP_RedirectDrives) ? 1 : 0;
1336  file->RdgIsKdcProxy = freerdp_settings_get_bool(settings, FreeRDP_KerberosRdgIsProxy) ? 1 : 0;
1337  file->RedirectComPorts = (freerdp_settings_get_bool(settings, FreeRDP_RedirectSerialPorts) ||
1338  freerdp_settings_get_bool(settings, FreeRDP_RedirectParallelPorts));
1339  file->RedirectLocation =
1340  freerdp_dynamic_channel_collection_find(settings, LOCATION_CHANNEL_NAME) ? TRUE : FALSE;
1341  if (!FILE_POPULATE_STRING(&file->DrivesToRedirect, settings, FreeRDP_DrivesToRedirect) ||
1342  !FILE_POPULATE_STRING(&file->PreconnectionBlob, settings, FreeRDP_PreconnectionBlob) ||
1343  !FILE_POPULATE_STRING(&file->KdcProxyName, settings, FreeRDP_KerberosKdcUrl))
1344  return FALSE;
1345 
1346  {
1347  size_t offset = 0;
1348  UINT32 count = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
1349  const UINT32* MonitorIds = freerdp_settings_get_pointer(settings, FreeRDP_MonitorIds);
1350  /* String size: 10 char UINT32 max string length, 1 char separator, one element NULL */
1351  size_t size = count * (10 + 1) + 1;
1352 
1353  char* str = calloc(size, sizeof(char));
1354  for (UINT32 x = 0; x < count; x++)
1355  {
1356  int rc = _snprintf(&str[offset], size - offset, "%" PRIu32 ",", MonitorIds[x]);
1357  if (rc <= 0)
1358  {
1359  free(str);
1360  return FALSE;
1361  }
1362  offset += (size_t)rc;
1363  }
1364  if (offset > 0)
1365  str[offset - 1] = '\0';
1366  freerdp_client_file_string_check_free(file->SelectedMonitors);
1367  file->SelectedMonitors = str;
1368  }
1369 
1370  file->KeyboardHook = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardHook);
1371 
1372  return TRUE;
1373 }
1374 
1375 BOOL freerdp_client_write_rdp_file(const rdpFile* file, const char* name, BOOL unicode)
1376 {
1377  int status = 0;
1378  WCHAR* unicodestr = NULL;
1379 
1380  if (!file || !name)
1381  return FALSE;
1382 
1383  const size_t size = freerdp_client_write_rdp_file_buffer(file, NULL, 0);
1384  if (size == 0)
1385  return FALSE;
1386  char* buffer = calloc(size + 1ULL, sizeof(char));
1387 
1388  if (freerdp_client_write_rdp_file_buffer(file, buffer, size + 1) != size)
1389  {
1390  WLog_ERR(TAG, "freerdp_client_write_rdp_file: error writing to output buffer");
1391  free(buffer);
1392  return FALSE;
1393  }
1394 
1395  FILE* fp = winpr_fopen(name, "w+b");
1396 
1397  if (fp)
1398  {
1399  if (unicode)
1400  {
1401  size_t len = 0;
1402  unicodestr = ConvertUtf8NToWCharAlloc(buffer, size, &len);
1403 
1404  if (!unicodestr)
1405  {
1406  free(buffer);
1407  (void)fclose(fp);
1408  return FALSE;
1409  }
1410 
1411  /* Write multi-byte header */
1412  if ((fwrite(BOM_UTF16_LE, sizeof(BYTE), 2, fp) != 2) ||
1413  (fwrite(unicodestr, sizeof(WCHAR), len, fp) != len))
1414  {
1415  free(buffer);
1416  free(unicodestr);
1417  (void)fclose(fp);
1418  return FALSE;
1419  }
1420 
1421  free(unicodestr);
1422  }
1423  else
1424  {
1425  if (fwrite(buffer, 1, size, fp) != size)
1426  {
1427  free(buffer);
1428  (void)fclose(fp);
1429  return FALSE;
1430  }
1431  }
1432 
1433  (void)fflush(fp);
1434  status = fclose(fp);
1435  }
1436 
1437  free(buffer);
1438  return (status == 0) ? TRUE : FALSE;
1439 }
1440 
1441 WINPR_ATTR_FORMAT_ARG(3, 4)
1442 static SSIZE_T freerdp_client_write_setting_to_buffer(char** buffer, size_t* bufferSize,
1443  WINPR_FORMAT_ARG const char* fmt, ...)
1444 {
1445  va_list ap = { 0 };
1446  SSIZE_T len = 0;
1447  char* buf = NULL;
1448  size_t bufSize = 0;
1449 
1450  if (!buffer || !bufferSize || !fmt)
1451  return -1;
1452 
1453  buf = *buffer;
1454  bufSize = *bufferSize;
1455 
1456  va_start(ap, fmt);
1457  len = vsnprintf(buf, bufSize, fmt, ap);
1458  va_end(ap);
1459  if (len < 0)
1460  return -1;
1461 
1462  /* _snprintf doesn't add the ending \0 to its return value */
1463  ++len;
1464 
1465  /* we just want to know the size - return it */
1466  if (!buf && !bufSize)
1467  return len;
1468 
1469  if (!buf)
1470  return -1;
1471 
1472  /* update buffer size and buffer position and replace \0 with \n */
1473  if (bufSize >= (size_t)len)
1474  {
1475  *bufferSize -= (size_t)len;
1476  buf[len - 1] = '\n';
1477  *buffer = buf + len;
1478  }
1479  else
1480  return -1;
1481 
1482  return len;
1483 }
1484 
1485 size_t freerdp_client_write_rdp_file_buffer(const rdpFile* file, char* buffer, size_t size)
1486 {
1487  size_t totalSize = 0;
1488 
1489  if (!file)
1490  return 0;
1491 
1492  /* either buffer and size are null or non-null */
1493  if ((!buffer || !size) && (buffer || size))
1494  return 0;
1495 
1496 #define WRITE_SETTING_(fmt_, ...) \
1497  { \
1498  SSIZE_T res = freerdp_client_write_setting_to_buffer(&buffer, &size, fmt_, __VA_ARGS__); \
1499  if (res < 0) \
1500  return 0; \
1501  totalSize += (size_t)res; \
1502  }
1503 
1504 #define WRITE_SETTING_INT(key_, param_) \
1505  do \
1506  { \
1507  if (~(param_)) \
1508  WRITE_SETTING_("%s:i:%" PRIu32, key_, param_) \
1509  } while (0)
1510 
1511 #define WRITE_SETTING_STR(key_, param_) \
1512  do \
1513  { \
1514  if (~(size_t)(param_)) \
1515  WRITE_SETTING_("%s:s:%s", key_, param_) \
1516  } while (0)
1517 
1518  /* integer parameters */
1519  WRITE_SETTING_INT(key_int_use_multimon, file->UseMultiMon);
1520  WRITE_SETTING_INT(key_int_maximizetocurrentdisplays, file->MaximizeToCurrentDisplays);
1521  WRITE_SETTING_INT(key_int_singlemoninwindowedmode, file->SingleMonInWindowedMode);
1522  WRITE_SETTING_INT(key_int_screen_mode_id, file->ScreenModeId);
1523  WRITE_SETTING_INT(key_int_span_monitors, file->SpanMonitors);
1524  WRITE_SETTING_INT(key_int_smart_sizing, file->SmartSizing);
1525  WRITE_SETTING_INT(key_int_dynamic_resolution, file->DynamicResolution);
1526  WRITE_SETTING_INT(key_int_enablesuperpan, file->EnableSuperSpan);
1527  WRITE_SETTING_INT(key_int_superpanaccelerationfactor, file->SuperSpanAccelerationFactor);
1528  WRITE_SETTING_INT(key_int_desktopwidth, file->DesktopWidth);
1529  WRITE_SETTING_INT(key_int_desktopheight, file->DesktopHeight);
1530  WRITE_SETTING_INT(key_int_desktop_size_id, file->DesktopSizeId);
1531  WRITE_SETTING_INT(key_int_session_bpp, file->SessionBpp);
1532  WRITE_SETTING_INT(key_int_desktopscalefactor, file->DesktopScaleFactor);
1533  WRITE_SETTING_INT(key_int_compression, file->Compression);
1534  WRITE_SETTING_INT(key_int_keyboardhook, file->KeyboardHook);
1535  WRITE_SETTING_INT(key_int_disable_ctrl_alt_del, file->DisableCtrlAltDel);
1536  WRITE_SETTING_INT(key_int_audiomode, file->AudioMode);
1537  WRITE_SETTING_INT(key_int_audioqualitymode, file->AudioQualityMode);
1538  WRITE_SETTING_INT(key_int_audiocapturemode, file->AudioCaptureMode);
1539  WRITE_SETTING_INT(key_int_encode_redirected_video_capture, file->EncodeRedirectedVideoCapture);
1540  WRITE_SETTING_INT(key_int_redirected_video_capture_encoding_quality,
1541  file->RedirectedVideoCaptureEncodingQuality);
1542  WRITE_SETTING_INT(key_int_videoplaybackmode, file->VideoPlaybackMode);
1543  WRITE_SETTING_INT(key_int_connection_type, file->ConnectionType);
1544  WRITE_SETTING_INT(key_int_networkautodetect, file->NetworkAutoDetect);
1545  WRITE_SETTING_INT(key_int_bandwidthautodetect, file->BandwidthAutoDetect);
1546  WRITE_SETTING_INT(key_int_pinconnectionbar, file->PinConnectionBar);
1547  WRITE_SETTING_INT(key_int_displayconnectionbar, file->DisplayConnectionBar);
1548  WRITE_SETTING_INT(key_int_workspaceid, file->WorkspaceId);
1549  WRITE_SETTING_INT(key_int_enableworkspacereconnect, file->EnableWorkspaceReconnect);
1550  WRITE_SETTING_INT(key_int_disable_wallpaper, file->DisableWallpaper);
1551  WRITE_SETTING_INT(key_int_allow_font_smoothing, file->AllowFontSmoothing);
1552  WRITE_SETTING_INT(key_int_allow_desktop_composition, file->AllowDesktopComposition);
1553  WRITE_SETTING_INT(key_int_disable_full_window_drag, file->DisableFullWindowDrag);
1554  WRITE_SETTING_INT(key_int_disable_menu_anims, file->DisableMenuAnims);
1555  WRITE_SETTING_INT(key_int_disable_themes, file->DisableThemes);
1556  WRITE_SETTING_INT(key_int_disable_cursor_setting, file->DisableCursorSetting);
1557  WRITE_SETTING_INT(key_int_bitmapcachesize, file->BitmapCacheSize);
1558  WRITE_SETTING_INT(key_int_bitmapcachepersistenable, file->BitmapCachePersistEnable);
1559  WRITE_SETTING_INT(key_int_server_port, file->ServerPort);
1560  WRITE_SETTING_INT(key_int_redirectdrives, file->RedirectDrives);
1561  WRITE_SETTING_INT(key_int_redirectprinters, file->RedirectPrinters);
1562  WRITE_SETTING_INT(key_int_redirectcomports, file->RedirectComPorts);
1563  WRITE_SETTING_INT(key_int_redirectlocation, file->RedirectLocation);
1564  WRITE_SETTING_INT(key_int_redirectsmartcards, file->RedirectSmartCards);
1565  WRITE_SETTING_INT(key_int_redirectclipboard, file->RedirectClipboard);
1566  WRITE_SETTING_INT(key_int_redirectposdevices, file->RedirectPosDevices);
1567  WRITE_SETTING_INT(key_int_redirectdirectx, file->RedirectDirectX);
1568  WRITE_SETTING_INT(key_int_disableprinterredirection, file->DisablePrinterRedirection);
1569  WRITE_SETTING_INT(key_int_disableclipboardredirection, file->DisableClipboardRedirection);
1570  WRITE_SETTING_INT(key_int_connect_to_console, file->ConnectToConsole);
1571  WRITE_SETTING_INT(key_int_administrative_session, file->AdministrativeSession);
1572  WRITE_SETTING_INT(key_int_autoreconnection_enabled, file->AutoReconnectionEnabled);
1573  WRITE_SETTING_INT(key_int_autoreconnect_max_retries, file->AutoReconnectMaxRetries);
1574  WRITE_SETTING_INT(key_int_public_mode, file->PublicMode);
1575  WRITE_SETTING_INT(key_int_authentication_level, file->AuthenticationLevel);
1576  WRITE_SETTING_INT(key_int_promptcredentialonce, file->PromptCredentialOnce);
1577  WRITE_SETTING_INT(key_int_prompt_for_credentials, file->PromptForCredentials);
1578  WRITE_SETTING_INT(key_int_negotiate_security_layer, file->NegotiateSecurityLayer);
1579  WRITE_SETTING_INT(key_int_enablecredsspsupport, file->EnableCredSSPSupport);
1580  WRITE_SETTING_INT(key_int_enablerdsaadauth, file->EnableRdsAadAuth);
1581  WRITE_SETTING_INT(key_int_remoteapplicationmode, file->RemoteApplicationMode);
1582  WRITE_SETTING_INT(key_int_remoteapplicationexpandcmdline, file->RemoteApplicationExpandCmdLine);
1583  WRITE_SETTING_INT(key_int_remoteapplicationexpandworkingdir,
1584  file->RemoteApplicationExpandWorkingDir);
1585  WRITE_SETTING_INT(key_int_disableconnectionsharing, file->DisableConnectionSharing);
1586  WRITE_SETTING_INT(key_int_disableremoteappcapscheck, file->DisableRemoteAppCapsCheck);
1587  WRITE_SETTING_INT(key_int_gatewayusagemethod, file->GatewayUsageMethod);
1588  WRITE_SETTING_INT(key_int_gatewayprofileusagemethod, file->GatewayProfileUsageMethod);
1589  WRITE_SETTING_INT(key_int_gatewaycredentialssource, file->GatewayCredentialsSource);
1590  WRITE_SETTING_INT(key_int_use_redirection_server_name, file->UseRedirectionServerName);
1591  WRITE_SETTING_INT(key_int_rdgiskdcproxy, file->RdgIsKdcProxy);
1592  WRITE_SETTING_INT(key_int_redirectwebauthn, file->RedirectWebauthN);
1593 
1594  /* string parameters */
1595  WRITE_SETTING_STR(key_str_username, file->Username);
1596  WRITE_SETTING_STR(key_str_domain, file->Domain);
1597  WRITE_SETTING_STR(key_str_password, file->Password);
1598  WRITE_SETTING_STR(key_str_full_address, file->FullAddress);
1599  WRITE_SETTING_STR(key_str_alternate_full_address, file->AlternateFullAddress);
1600  WRITE_SETTING_STR(key_str_usbdevicestoredirect, file->UsbDevicesToRedirect);
1601  WRITE_SETTING_STR(key_str_camerastoredirect, file->RedirectCameras);
1602  WRITE_SETTING_STR(key_str_loadbalanceinfo, file->LoadBalanceInfo);
1603  WRITE_SETTING_STR(key_str_remoteapplicationname, file->RemoteApplicationName);
1604  WRITE_SETTING_STR(key_str_remoteapplicationicon, file->RemoteApplicationIcon);
1605  WRITE_SETTING_STR(key_str_remoteapplicationprogram, file->RemoteApplicationProgram);
1606  WRITE_SETTING_STR(key_str_remoteapplicationfile, file->RemoteApplicationFile);
1607  WRITE_SETTING_STR(key_str_remoteapplicationguid, file->RemoteApplicationGuid);
1608  WRITE_SETTING_STR(key_str_remoteapplicationcmdline, file->RemoteApplicationCmdLine);
1609  WRITE_SETTING_STR(key_str_alternate_shell, file->AlternateShell);
1610  WRITE_SETTING_STR(key_str_shell_working_directory, file->ShellWorkingDirectory);
1611  WRITE_SETTING_STR(key_str_gatewayhostname, file->GatewayHostname);
1612  WRITE_SETTING_STR(key_str_resourceprovider, file->ResourceProvider);
1613  WRITE_SETTING_STR(key_str_wvd, file->WvdEndpointPool);
1614  WRITE_SETTING_STR(key_str_geo, file->geo);
1615  WRITE_SETTING_STR(key_str_armpath, file->armpath);
1616  WRITE_SETTING_STR(key_str_aadtenantid, file->aadtenantid);
1617  WRITE_SETTING_STR(key_str_diagnosticserviceurl, file->diagnosticserviceurl);
1618  WRITE_SETTING_STR(key_str_hubdiscoverygeourl, file->hubdiscoverygeourl);
1619  WRITE_SETTING_STR(key_str_activityhint, file->activityhint);
1620  WRITE_SETTING_STR(key_str_gatewayaccesstoken, file->GatewayAccessToken);
1621  WRITE_SETTING_STR(key_str_kdcproxyname, file->KdcProxyName);
1622  WRITE_SETTING_STR(key_str_drivestoredirect, file->DrivesToRedirect);
1623  WRITE_SETTING_STR(key_str_devicestoredirect, file->DevicesToRedirect);
1624  WRITE_SETTING_STR(key_str_winposstr, file->WinPosStr);
1625  WRITE_SETTING_STR(key_str_pcb, file->PreconnectionBlob);
1626  WRITE_SETTING_STR(key_str_selectedmonitors, file->SelectedMonitors);
1627 
1628  /* custom parameters */
1629  for (size_t i = 0; i < file->lineCount; ++i)
1630  {
1631  SSIZE_T res = -1;
1632  const rdpFileLine* curLine = &file->lines[i];
1633 
1634  if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_INTEGER)
1635  res = freerdp_client_write_setting_to_buffer(&buffer, &size, "%s:i:%" PRIu32,
1636  curLine->name, (UINT32)curLine->iValue);
1637  else if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_STRING)
1638  res = freerdp_client_write_setting_to_buffer(&buffer, &size, "%s:s:%s", curLine->name,
1639  curLine->sValue);
1640  if (res < 0)
1641  return 0;
1642 
1643  totalSize += (size_t)res;
1644  }
1645 
1646  return totalSize;
1647 }
1648 
1649 static ADDIN_ARGV* rdp_file_to_args(const char* channel, const char* values)
1650 {
1651  size_t count = 0;
1652  char** p = NULL;
1653  ADDIN_ARGV* args = freerdp_addin_argv_new(0, NULL);
1654  if (!args)
1655  return NULL;
1656  if (!freerdp_addin_argv_add_argument(args, channel))
1657  goto fail;
1658 
1659  p = CommandLineParseCommaSeparatedValues(values, &count);
1660  for (size_t x = 0; x < count; x++)
1661  {
1662  BOOL rc = 0;
1663  const char* val = p[x];
1664  const size_t len = strlen(val) + 8;
1665  char* str = calloc(len, sizeof(char));
1666  if (!str)
1667  goto fail;
1668 
1669  (void)_snprintf(str, len, "device:%s", val);
1670  rc = freerdp_addin_argv_add_argument(args, str);
1671  free(str);
1672  if (!rc)
1673  goto fail;
1674  }
1675  CommandLineParserFree(p);
1676  return args;
1677 
1678 fail:
1679  CommandLineParserFree(p);
1680  freerdp_addin_argv_free(args);
1681  return NULL;
1682 }
1683 
1684 BOOL freerdp_client_populate_settings_from_rdp_file(const rdpFile* file, rdpSettings* settings)
1685 {
1686  BOOL setDefaultConnectionType = TRUE;
1687 
1688  if (!file || !settings)
1689  return FALSE;
1690 
1691  if (~((size_t)file->Domain))
1692  {
1693  if (!freerdp_settings_set_string(settings, FreeRDP_Domain, file->Domain))
1694  return FALSE;
1695  }
1696 
1697  if (~((size_t)file->Username))
1698  {
1699  char* user = NULL;
1700  char* domain = NULL;
1701 
1702  if (!freerdp_parse_username(file->Username, &user, &domain))
1703  return FALSE;
1704 
1705  if (!freerdp_settings_set_string(settings, FreeRDP_Username, user))
1706  return FALSE;
1707 
1708  if (!(~((size_t)file->Domain)) && domain)
1709  {
1710  if (!freerdp_settings_set_string(settings, FreeRDP_Domain, domain))
1711  return FALSE;
1712  }
1713 
1714  free(user);
1715  free(domain);
1716  }
1717 
1718  if (~((size_t)file->Password))
1719  {
1720  if (!freerdp_settings_set_string(settings, FreeRDP_Password, file->Password))
1721  return FALSE;
1722  }
1723 
1724  {
1725  const char* address = NULL;
1726 
1727  /* With MSTSC alternate full address always wins,
1728  * so mimic this. */
1729  if (~((size_t)file->AlternateFullAddress))
1730  address = file->AlternateFullAddress;
1731  else if (~((size_t)file->FullAddress))
1732  address = file->FullAddress;
1733 
1734  if (address)
1735  {
1736  int port = -1;
1737  char* host = NULL;
1738 
1739  if (!freerdp_parse_hostname(address, &host, &port))
1740  return FALSE;
1741 
1742  const BOOL rc = freerdp_settings_set_string(settings, FreeRDP_ServerHostname, host);
1743  free(host);
1744  if (!rc)
1745  return FALSE;
1746 
1747  if (port > 0)
1748  {
1749  if (!freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, (UINT32)port))
1750  return FALSE;
1751  }
1752  }
1753  }
1754 
1755  if (~file->ServerPort)
1756  {
1757  if (!freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, file->ServerPort))
1758  return FALSE;
1759  }
1760 
1761  if (~file->DesktopSizeId)
1762  {
1763  switch (file->DesktopSizeId)
1764  {
1765  case 0:
1766  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 640))
1767  return FALSE;
1768  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 480))
1769  return FALSE;
1770  break;
1771  case 1:
1772  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 800))
1773  return FALSE;
1774  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 600))
1775  return FALSE;
1776  break;
1777  case 2:
1778  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1024))
1779  return FALSE;
1780  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 768))
1781  return FALSE;
1782  break;
1783  case 3:
1784  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1280))
1785  return FALSE;
1786  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 1024))
1787  return FALSE;
1788  break;
1789  case 4:
1790  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1600))
1791  return FALSE;
1792  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 1200))
1793  return FALSE;
1794  break;
1795  default:
1796  WLog_WARN(TAG, "Unsupported 'desktop size id' value %" PRIu32, file->DesktopSizeId);
1797  break;
1798  }
1799  }
1800 
1801  if (~file->DesktopWidth)
1802  {
1803  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, file->DesktopWidth))
1804  return FALSE;
1805  }
1806 
1807  if (~file->DesktopHeight)
1808  {
1809  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, file->DesktopHeight))
1810  return FALSE;
1811  }
1812 
1813  if (~file->SessionBpp)
1814  {
1815  if (!freerdp_settings_set_uint32(settings, FreeRDP_ColorDepth, file->SessionBpp))
1816  return FALSE;
1817  }
1818 
1819  if (~file->ConnectToConsole)
1820  {
1821  if (!freerdp_settings_set_bool(settings, FreeRDP_ConsoleSession,
1822  file->ConnectToConsole != 0))
1823  return FALSE;
1824  }
1825 
1826  if (~file->AdministrativeSession)
1827  {
1828  if (!freerdp_settings_set_bool(settings, FreeRDP_ConsoleSession,
1829  file->AdministrativeSession != 0))
1830  return FALSE;
1831  }
1832 
1833  if (~file->NegotiateSecurityLayer)
1834  {
1835  if (!freerdp_settings_set_bool(settings, FreeRDP_NegotiateSecurityLayer,
1836  file->NegotiateSecurityLayer != 0))
1837  return FALSE;
1838  }
1839 
1840  if (~file->EnableCredSSPSupport)
1841  {
1842  if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity,
1843  file->EnableCredSSPSupport != 0))
1844  return FALSE;
1845  }
1846 
1847  if (~file->EnableRdsAadAuth)
1848  {
1849  if (!freerdp_settings_set_bool(settings, FreeRDP_AadSecurity, file->EnableRdsAadAuth != 0))
1850  return FALSE;
1851  }
1852 
1853  if (~((size_t)file->AlternateShell))
1854  {
1855  if (!freerdp_settings_set_string(settings, FreeRDP_AlternateShell, file->AlternateShell))
1856  return FALSE;
1857  }
1858 
1859  if (~((size_t)file->ShellWorkingDirectory))
1860  {
1861  /* ShellWorkingDir is used for either, shell working dir or remote app working dir */
1862  FreeRDP_Settings_Keys_String targetId =
1863  (~file->RemoteApplicationMode && file->RemoteApplicationMode != 0)
1864  ? FreeRDP_RemoteApplicationWorkingDir
1865  : FreeRDP_ShellWorkingDirectory;
1866 
1867  if (!freerdp_settings_set_string(settings, targetId, file->ShellWorkingDirectory))
1868  return FALSE;
1869  }
1870 
1871  if (~file->ScreenModeId)
1872  {
1885  if (!freerdp_settings_set_bool(settings, FreeRDP_Fullscreen,
1886  (file->ScreenModeId == 2) ? TRUE : FALSE))
1887  return FALSE;
1888  }
1889 
1890  if (~(file->SmartSizing))
1891  {
1892  if (!freerdp_settings_set_bool(settings, FreeRDP_SmartSizing,
1893  (file->SmartSizing == 1) ? TRUE : FALSE))
1894  return FALSE;
1904  if (((~(file->DesktopWidth) && ~(file->DesktopHeight)) || ~(file->DesktopSizeId)) &&
1905  (file->SmartSizing == 1))
1906  {
1907  if (!freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingWidth,
1908  file->DesktopWidth))
1909  return FALSE;
1910  if (!freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingHeight,
1911  file->DesktopHeight))
1912  return FALSE;
1913  }
1914  }
1915 
1916  if (~((size_t)file->LoadBalanceInfo))
1917  {
1918  const size_t len = strlen(file->LoadBalanceInfo);
1919  if (!freerdp_settings_set_pointer_len(settings, FreeRDP_LoadBalanceInfo,
1920  file->LoadBalanceInfo, len))
1921  return FALSE;
1922  }
1923 
1924  if (~file->AuthenticationLevel)
1925  {
1940  if (!freerdp_settings_set_uint32(settings, FreeRDP_AuthenticationLevel,
1941  file->AuthenticationLevel))
1942  return FALSE;
1943  }
1944 
1945  if (~file->ConnectionType)
1946  {
1947  if (!freerdp_set_connection_type(settings, file->ConnectionType))
1948  return FALSE;
1949  setDefaultConnectionType = FALSE;
1950  }
1951 
1952  if (~file->AudioMode)
1953  {
1954  switch (file->AudioMode)
1955  {
1956  case AUDIO_MODE_REDIRECT:
1957  if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, FALSE))
1958  return FALSE;
1959  if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, TRUE))
1960  return FALSE;
1961  break;
1962  case AUDIO_MODE_PLAY_ON_SERVER:
1963  if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, TRUE))
1964  return FALSE;
1965  if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, FALSE))
1966  return FALSE;
1967  break;
1968  case AUDIO_MODE_NONE:
1969  default:
1970  if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, FALSE))
1971  return FALSE;
1972  if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, FALSE))
1973  return FALSE;
1974  break;
1975  }
1976  }
1977 
1978  if (~file->AudioCaptureMode)
1979  {
1980  if (!freerdp_settings_set_bool(settings, FreeRDP_AudioCapture, file->AudioCaptureMode != 0))
1981  return FALSE;
1982  }
1983 
1984  if (~file->Compression)
1985  {
1986  if (!freerdp_settings_set_bool(settings, FreeRDP_CompressionEnabled,
1987  file->Compression != 0))
1988  return FALSE;
1989  }
1990 
1991  if (~((size_t)file->GatewayHostname))
1992  {
1993  int port = -1;
1994  char* host = NULL;
1995 
1996  if (!freerdp_parse_hostname(file->GatewayHostname, &host, &port))
1997  return FALSE;
1998 
1999  const BOOL rc = freerdp_settings_set_string(settings, FreeRDP_GatewayHostname, host);
2000  free(host);
2001  if (!rc)
2002  return FALSE;
2003 
2004  if (port > 0)
2005  {
2006  if (!freerdp_settings_set_uint32(settings, FreeRDP_GatewayPort, (UINT32)port))
2007  return FALSE;
2008  }
2009  }
2010 
2011  if (~((size_t)file->ResourceProvider))
2012  {
2013  if (_stricmp(file->ResourceProvider, str_resourceprovider_arm) == 0)
2014  {
2015  if (!freerdp_settings_set_bool(settings, FreeRDP_GatewayArmTransport, TRUE))
2016  return FALSE;
2017  }
2018  }
2019 
2020  if (~((size_t)file->WvdEndpointPool))
2021  {
2022  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdWvdEndpointPool,
2023  file->WvdEndpointPool))
2024  return FALSE;
2025  }
2026 
2027  if (~((size_t)file->geo))
2028  {
2029  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdGeo, file->geo))
2030  return FALSE;
2031  }
2032 
2033  if (~((size_t)file->armpath))
2034  {
2035  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdArmpath, file->armpath))
2036  return FALSE;
2037  }
2038 
2039  if (~((size_t)file->aadtenantid))
2040  {
2041  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdAadtenantid,
2042  file->aadtenantid))
2043  return FALSE;
2044  }
2045 
2046  if (~((size_t)file->diagnosticserviceurl))
2047  {
2048  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdDiagnosticserviceurl,
2049  file->diagnosticserviceurl))
2050  return FALSE;
2051  }
2052 
2053  if (~((size_t)file->hubdiscoverygeourl))
2054  {
2055  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdHubdiscoverygeourl,
2056  file->hubdiscoverygeourl))
2057  return FALSE;
2058  }
2059 
2060  if (~((size_t)file->activityhint))
2061  {
2062  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdActivityhint,
2063  file->activityhint))
2064  return FALSE;
2065  }
2066 
2067  if (~((size_t)file->GatewayAccessToken))
2068  {
2069  if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAccessToken,
2070  file->GatewayAccessToken))
2071  return FALSE;
2072  }
2073 
2074  if (~file->GatewayUsageMethod)
2075  {
2076  if (!freerdp_set_gateway_usage_method(settings, file->GatewayUsageMethod))
2077  return FALSE;
2078  }
2079 
2080  if (~file->PromptCredentialOnce)
2081  {
2082  if (!freerdp_settings_set_bool(settings, FreeRDP_GatewayUseSameCredentials,
2083  file->PromptCredentialOnce != 0))
2084  return FALSE;
2085  }
2086 
2087  if (~file->PromptForCredentials)
2088  {
2089  if (!freerdp_settings_set_bool(settings, FreeRDP_PromptForCredentials,
2090  file->PromptForCredentials != 0))
2091  return FALSE;
2092  }
2093 
2094  if (~file->RemoteApplicationMode)
2095  {
2096  if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteApplicationMode,
2097  file->RemoteApplicationMode != 0))
2098  return FALSE;
2099  }
2100 
2101  if (~((size_t)file->RemoteApplicationProgram))
2102  {
2103  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationProgram,
2104  file->RemoteApplicationProgram))
2105  return FALSE;
2106  }
2107 
2108  if (~((size_t)file->RemoteApplicationName))
2109  {
2110  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationName,
2111  file->RemoteApplicationName))
2112  return FALSE;
2113  }
2114 
2115  if (~((size_t)file->RemoteApplicationIcon))
2116  {
2117  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationIcon,
2118  file->RemoteApplicationIcon))
2119  return FALSE;
2120  }
2121 
2122  if (~((size_t)file->RemoteApplicationFile))
2123  {
2124  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationFile,
2125  file->RemoteApplicationFile))
2126  return FALSE;
2127  }
2128 
2129  if (~((size_t)file->RemoteApplicationGuid))
2130  {
2131  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationGuid,
2132  file->RemoteApplicationGuid))
2133  return FALSE;
2134  }
2135 
2136  if (~((size_t)file->RemoteApplicationCmdLine))
2137  {
2138  if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationCmdLine,
2139  file->RemoteApplicationCmdLine))
2140  return FALSE;
2141  }
2142 
2143  if (~file->SpanMonitors)
2144  {
2145  if (!freerdp_settings_set_bool(settings, FreeRDP_SpanMonitors, file->SpanMonitors != 0))
2146  return FALSE;
2147  }
2148 
2149  if (~file->UseMultiMon)
2150  {
2151  if (!freerdp_settings_set_bool(settings, FreeRDP_UseMultimon, file->UseMultiMon != 0))
2152  return FALSE;
2153  }
2154 
2155  if (~file->AllowFontSmoothing)
2156  {
2157  if (!freerdp_settings_set_bool(settings, FreeRDP_AllowFontSmoothing,
2158  file->AllowFontSmoothing != 0))
2159  return FALSE;
2160  }
2161 
2162  if (~file->DisableWallpaper)
2163  {
2164  if (!freerdp_settings_set_bool(settings, FreeRDP_DisableWallpaper,
2165  file->DisableWallpaper != 0))
2166  return FALSE;
2167  }
2168 
2169  if (~file->DisableFullWindowDrag)
2170  {
2171  if (!freerdp_settings_set_bool(settings, FreeRDP_DisableFullWindowDrag,
2172  file->DisableFullWindowDrag != 0))
2173  return FALSE;
2174  }
2175 
2176  if (~file->DisableMenuAnims)
2177  {
2178  if (!freerdp_settings_set_bool(settings, FreeRDP_DisableMenuAnims,
2179  file->DisableMenuAnims != 0))
2180  return FALSE;
2181  }
2182 
2183  if (~file->DisableThemes)
2184  {
2185  if (!freerdp_settings_set_bool(settings, FreeRDP_DisableThemes, file->DisableThemes != 0))
2186  return FALSE;
2187  }
2188 
2189  if (~file->AllowDesktopComposition)
2190  {
2191  if (!freerdp_settings_set_bool(settings, FreeRDP_AllowDesktopComposition,
2192  file->AllowDesktopComposition != 0))
2193  return FALSE;
2194  }
2195 
2196  if (~file->BitmapCachePersistEnable)
2197  {
2198  if (!freerdp_settings_set_bool(settings, FreeRDP_BitmapCachePersistEnabled,
2199  file->BitmapCachePersistEnable != 0))
2200  return FALSE;
2201  }
2202 
2203  if (~file->DisableRemoteAppCapsCheck)
2204  {
2205  if (!freerdp_settings_set_bool(settings, FreeRDP_DisableRemoteAppCapsCheck,
2206  file->DisableRemoteAppCapsCheck != 0))
2207  return FALSE;
2208  }
2209 
2210  if (~file->BandwidthAutoDetect)
2211  {
2212  if (file->BandwidthAutoDetect != 0)
2213  {
2214  if ((~file->NetworkAutoDetect) && (file->NetworkAutoDetect == 0))
2215  {
2216  WLog_WARN(TAG,
2217  "Got networkautodetect:i:%" PRIu32 " and bandwidthautodetect:i:%" PRIu32
2218  ". Correcting to networkautodetect:i:1",
2219  file->NetworkAutoDetect, file->BandwidthAutoDetect);
2220  WLog_WARN(TAG,
2221  "Add networkautodetect:i:1 to your RDP file to eliminate this warning.");
2222  }
2223 
2224  if (!freerdp_set_connection_type(settings, CONNECTION_TYPE_AUTODETECT))
2225  return FALSE;
2226  setDefaultConnectionType = FALSE;
2227  }
2228  if (!freerdp_settings_set_bool(settings, FreeRDP_NetworkAutoDetect,
2229  (file->BandwidthAutoDetect != 0) ||
2230  (file->NetworkAutoDetect != 0)))
2231  return FALSE;
2232  }
2233 
2234  if (~file->NetworkAutoDetect)
2235  {
2236  if (file->NetworkAutoDetect != 0)
2237  {
2238  if ((~file->BandwidthAutoDetect) && (file->BandwidthAutoDetect == 0))
2239  {
2240  WLog_WARN(TAG,
2241  "Got networkautodetect:i:%" PRIu32 " and bandwidthautodetect:i:%" PRIu32
2242  ". Correcting to bandwidthautodetect:i:1",
2243  file->NetworkAutoDetect, file->BandwidthAutoDetect);
2244  WLog_WARN(
2245  TAG, "Add bandwidthautodetect:i:1 to your RDP file to eliminate this warning.");
2246  }
2247 
2248  if (!freerdp_set_connection_type(settings, CONNECTION_TYPE_AUTODETECT))
2249  return FALSE;
2250 
2251  setDefaultConnectionType = FALSE;
2252  }
2253  if (!freerdp_settings_set_bool(settings, FreeRDP_NetworkAutoDetect,
2254  (file->BandwidthAutoDetect != 0) ||
2255  (file->NetworkAutoDetect != 0)))
2256  return FALSE;
2257  }
2258 
2259  if (~file->AutoReconnectionEnabled)
2260  {
2261  if (!freerdp_settings_set_bool(settings, FreeRDP_AutoReconnectionEnabled,
2262  file->AutoReconnectionEnabled != 0))
2263  return FALSE;
2264  }
2265 
2266  if (~file->AutoReconnectMaxRetries)
2267  {
2268  if (!freerdp_settings_set_uint32(settings, FreeRDP_AutoReconnectMaxRetries,
2269  file->AutoReconnectMaxRetries))
2270  return FALSE;
2271  }
2272 
2273  if (~file->RedirectSmartCards)
2274  {
2275  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSmartCards,
2276  file->RedirectSmartCards != 0))
2277  return FALSE;
2278  }
2279 
2280  if (~file->RedirectWebauthN)
2281  {
2282  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectWebAuthN,
2283  file->RedirectWebauthN != 0))
2284  return FALSE;
2285  }
2286 
2287  if (~file->RedirectClipboard)
2288  {
2289  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard,
2290  file->RedirectClipboard != 0))
2291  return FALSE;
2292  }
2293 
2294  if (~file->RedirectPrinters)
2295  {
2296  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters,
2297  file->RedirectPrinters != 0))
2298  return FALSE;
2299  }
2300 
2301  if (~file->RedirectDrives)
2302  {
2303  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, file->RedirectDrives != 0))
2304  return FALSE;
2305  }
2306 
2307  if (~file->RedirectPosDevices)
2308  {
2309  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts,
2310  file->RedirectComPorts != 0) ||
2311  !freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts,
2312  file->RedirectComPorts != 0))
2313  return FALSE;
2314  }
2315 
2316  if (~file->RedirectComPorts)
2317  {
2318  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts,
2319  file->RedirectComPorts != 0) ||
2320  !freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts,
2321  file->RedirectComPorts != 0))
2322  return FALSE;
2323  }
2324 
2325  if (~file->RedirectLocation && (file->RedirectLocation != 0))
2326  {
2327  size_t count = 0;
2328 
2329  char** ptr = CommandLineParseCommaSeparatedValuesEx(LOCATION_CHANNEL_NAME, NULL, &count);
2330  const BOOL rc =
2331  freerdp_client_add_dynamic_channel(settings, count, (const char* const*)ptr);
2332  CommandLineParserFree(ptr);
2333  if (!rc)
2334  return FALSE;
2335  }
2336 
2337  if (~file->RedirectDirectX)
2338  {
2339  /* What is this?! */
2340  }
2341 
2342  if ((~((size_t)file->DevicesToRedirect)) && !utils_str_is_empty(file->DevicesToRedirect))
2343  {
2368  if (!freerdp_settings_set_bool(settings, FreeRDP_DeviceRedirection, TRUE))
2369  return FALSE;
2370  }
2371 
2372  if ((~((size_t)file->DrivesToRedirect)) && !utils_str_is_empty(file->DrivesToRedirect))
2373  {
2374  if (!freerdp_settings_set_string(settings, FreeRDP_DrivesToRedirect,
2375  file->DrivesToRedirect))
2376  return FALSE;
2377  }
2378 
2379  if ((~((size_t)file->RedirectCameras)) && !utils_str_is_empty(file->RedirectCameras))
2380  {
2381 #if defined(CHANNEL_RDPECAM_CLIENT)
2382  union
2383  {
2384  char** c;
2385  const char* const* cc;
2386  } cnv;
2387  ADDIN_ARGV* args = rdp_file_to_args(RDPECAM_DVC_CHANNEL_NAME, file->RedirectCameras);
2388  if (!args)
2389  return FALSE;
2390 
2391  BOOL status = TRUE;
2392  if (~file->EncodeRedirectedVideoCapture)
2393  {
2394  char encode[64] = { 0 };
2395  (void)_snprintf(encode, sizeof(encode), "encode:%" PRIu32,
2396  file->EncodeRedirectedVideoCapture);
2397  if (!freerdp_addin_argv_add_argument(args, encode))
2398  status = FALSE;
2399  }
2400  if (~file->RedirectedVideoCaptureEncodingQuality)
2401  {
2402  char quality[64] = { 0 };
2403  (void)_snprintf(quality, sizeof(quality), "quality:%" PRIu32,
2404  file->RedirectedVideoCaptureEncodingQuality);
2405  if (!freerdp_addin_argv_add_argument(args, quality))
2406  status = FALSE;
2407  }
2408 
2409  cnv.c = args->argv;
2410  if (status)
2411  status = freerdp_client_add_dynamic_channel(
2412  settings, WINPR_ASSERTING_INT_CAST(size_t, args->argc), cnv.cc);
2413  freerdp_addin_argv_free(args);
2414  if (!status)
2415  return FALSE;
2416 #else
2417  WLog_WARN(
2418  TAG,
2419  "This build does not support [MS-RDPECAM] camera redirection channel. Ignoring '%s'",
2420  key_str_camerastoredirect);
2421 #endif
2422  }
2423 
2424  if ((~((size_t)file->UsbDevicesToRedirect)) && !utils_str_is_empty(file->UsbDevicesToRedirect))
2425  {
2426 #ifdef CHANNEL_URBDRC_CLIENT
2427  union
2428  {
2429  char** c;
2430  const char* const* cc;
2431  } cnv;
2432  ADDIN_ARGV* args = rdp_file_to_args(URBDRC_CHANNEL_NAME, file->UsbDevicesToRedirect);
2433  if (!args)
2434  return FALSE;
2435  cnv.c = args->argv;
2436  const BOOL status = freerdp_client_add_dynamic_channel(
2437  settings, WINPR_ASSERTING_INT_CAST(size_t, args->argc), cnv.cc);
2438  freerdp_addin_argv_free(args);
2439  if (!status)
2440  return FALSE;
2441 #else
2442  WLog_WARN(TAG,
2443  "This build does not support [MS-RDPEUSB] usb redirection channel. Ignoring '%s'",
2444  key_str_usbdevicestoredirect);
2445 #endif
2446  }
2447 
2448  if (~file->KeyboardHook)
2449  {
2450  if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardHook, file->KeyboardHook))
2451  return FALSE;
2452  }
2453 
2454  if (~(size_t)file->SelectedMonitors)
2455  {
2456  size_t count = 0;
2457  char** ptr = CommandLineParseCommaSeparatedValues(file->SelectedMonitors, &count);
2458  UINT32* list = NULL;
2459 
2460  if (!freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorIds, NULL, count))
2461  {
2462  CommandLineParserFree(ptr);
2463  return FALSE;
2464  }
2465  list = freerdp_settings_get_pointer_writable(settings, FreeRDP_MonitorIds);
2466  if (!list && (count > 0))
2467  {
2468  CommandLineParserFree(ptr);
2469  return FALSE;
2470  }
2471  for (size_t x = 0; x < count; x++)
2472  {
2473  unsigned long val = 0;
2474  errno = 0;
2475  val = strtoul(ptr[x], NULL, 0);
2476  if ((val >= UINT32_MAX) && (errno != 0))
2477  {
2478  CommandLineParserFree(ptr);
2479  free(list);
2480  return FALSE;
2481  }
2482  list[x] = (UINT32)val;
2483  }
2484  CommandLineParserFree(ptr);
2485  }
2486 
2487  if (~file->DynamicResolution)
2488  {
2489  const BOOL val = file->DynamicResolution != 0;
2490  if (val)
2491  {
2492  if (!freerdp_settings_set_bool(settings, FreeRDP_SupportDisplayControl, TRUE))
2493  return FALSE;
2494  }
2495  if (!freerdp_settings_set_bool(settings, FreeRDP_DynamicResolutionUpdate, val))
2496  return FALSE;
2497  }
2498 
2499  if (~file->DesktopScaleFactor)
2500  {
2501  if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopScaleFactor,
2502  file->DesktopScaleFactor))
2503  return FALSE;
2504  }
2505 
2506  if (~file->VideoPlaybackMode)
2507  {
2508  if (file->VideoPlaybackMode != 0)
2509  {
2510  if (!freerdp_settings_set_bool(settings, FreeRDP_SupportGeometryTracking, TRUE) ||
2511  !freerdp_settings_set_bool(settings, FreeRDP_SupportVideoOptimized, TRUE))
2512  return FALSE;
2513  }
2514  else
2515  {
2516  if (!freerdp_settings_set_bool(settings, FreeRDP_SupportVideoOptimized, FALSE))
2517  return FALSE;
2518  }
2519  }
2520  // TODO file->MaximizeToCurrentDisplays;
2521  // TODO file->SingleMonInWindowedMode;
2522  // TODO file->EncodeRedirectedVideoCapture;
2523  // TODO file->RedirectedVideoCaptureEncodingQuality;
2524 
2525  if (~((size_t)file->PreconnectionBlob))
2526  {
2527  if (!freerdp_settings_set_string(settings, FreeRDP_PreconnectionBlob,
2528  file->PreconnectionBlob) ||
2529  !freerdp_settings_set_bool(settings, FreeRDP_SendPreconnectionPdu, TRUE))
2530  return FALSE;
2531  }
2532 
2533  if (~((size_t)file->KdcProxyName))
2534  {
2535  if (!freerdp_settings_set_string(settings, FreeRDP_KerberosKdcUrl, file->KdcProxyName))
2536  return FALSE;
2537  }
2538 
2539  if (~((size_t)file->RdgIsKdcProxy))
2540  {
2541  if (!freerdp_settings_set_bool(settings, FreeRDP_KerberosRdgIsProxy,
2542  file->RdgIsKdcProxy != 0))
2543  return FALSE;
2544  }
2545 
2546  if (file->args->argc > 1)
2547  {
2548  WCHAR* ConnectionFile =
2549  freerdp_settings_get_string_as_utf16(settings, FreeRDP_ConnectionFile, NULL);
2550 
2551  if (freerdp_client_settings_parse_command_line(settings, file->args->argc, file->args->argv,
2552  FALSE) < 0)
2553  {
2554  free(ConnectionFile);
2555  return FALSE;
2556  }
2557 
2558  BOOL rc = freerdp_settings_set_string_from_utf16(settings, FreeRDP_ConnectionFile,
2559  ConnectionFile);
2560  free(ConnectionFile);
2561  if (!rc)
2562  return FALSE;
2563  }
2564 
2565  if (setDefaultConnectionType)
2566  {
2567  if (!freerdp_set_connection_type(settings, CONNECTION_TYPE_AUTODETECT))
2568  return FALSE;
2569  }
2570 
2571  return TRUE;
2572 }
2573 
2574 static rdpFileLine* freerdp_client_rdp_file_find_line_by_name(const rdpFile* file, const char* name)
2575 {
2576  BOOL bFound = FALSE;
2577  rdpFileLine* line = NULL;
2578 
2579  for (size_t index = 0; index < file->lineCount; index++)
2580  {
2581  line = &(file->lines[index]);
2582 
2583  if (line->flags & RDP_FILE_LINE_FLAG_FORMATTED)
2584  {
2585  if (_stricmp(name, line->name) == 0)
2586  {
2587  bFound = TRUE;
2588  break;
2589  }
2590  }
2591  }
2592 
2593  return (bFound) ? line : NULL;
2594 }
2602 int freerdp_client_rdp_file_set_string_option(rdpFile* file, const char* name, const char* value)
2603 {
2604  return freerdp_client_rdp_file_set_string(file, name, value);
2605 }
2606 
2607 const char* freerdp_client_rdp_file_get_string_option(const rdpFile* file, const char* name)
2608 {
2609  LPSTR* value = NULL;
2610  rdpFileLine* line = NULL;
2611 
2612  rdpFile* wfile = WINPR_CAST_CONST_PTR_AWAY(file, rdpFile*);
2613  if (freerdp_client_rdp_file_find_string_entry(wfile, name, &value, &line))
2614  {
2615  if (value && ~(size_t)(*value))
2616  return *value;
2617  if (line)
2618  return line->sValue;
2619  }
2620 
2621  return NULL;
2622 }
2623 
2624 int freerdp_client_rdp_file_set_integer_option(rdpFile* file, const char* name, int value)
2625 {
2626  return freerdp_client_rdp_file_set_integer(file, name, value);
2627 }
2628 
2629 int freerdp_client_rdp_file_get_integer_option(const rdpFile* file, const char* name)
2630 {
2631  DWORD* value = NULL;
2632  rdpFileLine* line = NULL;
2633 
2634  rdpFile* wfile = WINPR_CAST_CONST_PTR_AWAY(file, rdpFile*);
2635  if (freerdp_client_rdp_file_find_integer_entry(wfile, name, &value, &line))
2636  {
2637  if (value && ~(*value))
2638  return WINPR_ASSERTING_INT_CAST(int, *value);
2639  if (line)
2640  return (int)line->iValue;
2641  }
2642 
2643  return -1;
2644 }
2645 
2646 static void freerdp_client_file_string_check_free(LPSTR str)
2647 {
2648  if (~((size_t)str))
2649  free(str);
2650 }
2651 
2652 rdpFile* freerdp_client_rdp_file_new(void)
2653 {
2654  return freerdp_client_rdp_file_new_ex(0);
2655 }
2656 
2657 rdpFile* freerdp_client_rdp_file_new_ex(DWORD flags)
2658 {
2659  rdpFile* file = (rdpFile*)calloc(1, sizeof(rdpFile));
2660 
2661  if (!file)
2662  return NULL;
2663 
2664  file->flags = flags;
2665 
2666  FillMemory(file, sizeof(rdpFile), 0xFF);
2667  file->lines = NULL;
2668  file->lineCount = 0;
2669  file->lineSize = 32;
2670  file->GatewayProfileUsageMethod = 1;
2671  file->lines = (rdpFileLine*)calloc(file->lineSize, sizeof(rdpFileLine));
2672 
2673  file->args = freerdp_addin_argv_new(0, NULL);
2674  if (!file->lines || !file->args)
2675  goto fail;
2676 
2677  if (!freerdp_client_add_option(file, "freerdp"))
2678  goto fail;
2679 
2680  return file;
2681 fail:
2682  WINPR_PRAGMA_DIAG_PUSH
2683  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2684  freerdp_client_rdp_file_free(file);
2685  WINPR_PRAGMA_DIAG_POP
2686  return NULL;
2687 }
2688 void freerdp_client_rdp_file_free(rdpFile* file)
2689 {
2690  if (file)
2691  {
2692  if (file->lineCount)
2693  {
2694  for (size_t i = 0; i < file->lineCount; i++)
2695  {
2696  free(file->lines[i].name);
2697  free(file->lines[i].sValue);
2698  }
2699  }
2700  free(file->lines);
2701 
2702  freerdp_addin_argv_free(file->args);
2703 
2704  freerdp_client_file_string_check_free(file->Username);
2705  freerdp_client_file_string_check_free(file->Domain);
2706  freerdp_client_file_string_check_free(file->Password);
2707  freerdp_client_file_string_check_free(file->FullAddress);
2708  freerdp_client_file_string_check_free(file->AlternateFullAddress);
2709  freerdp_client_file_string_check_free(file->UsbDevicesToRedirect);
2710  freerdp_client_file_string_check_free(file->RedirectCameras);
2711  freerdp_client_file_string_check_free(file->SelectedMonitors);
2712  freerdp_client_file_string_check_free(file->LoadBalanceInfo);
2713  freerdp_client_file_string_check_free(file->RemoteApplicationName);
2714  freerdp_client_file_string_check_free(file->RemoteApplicationIcon);
2715  freerdp_client_file_string_check_free(file->RemoteApplicationProgram);
2716  freerdp_client_file_string_check_free(file->RemoteApplicationFile);
2717  freerdp_client_file_string_check_free(file->RemoteApplicationGuid);
2718  freerdp_client_file_string_check_free(file->RemoteApplicationCmdLine);
2719  freerdp_client_file_string_check_free(file->AlternateShell);
2720  freerdp_client_file_string_check_free(file->ShellWorkingDirectory);
2721  freerdp_client_file_string_check_free(file->GatewayHostname);
2722  freerdp_client_file_string_check_free(file->GatewayAccessToken);
2723  freerdp_client_file_string_check_free(file->KdcProxyName);
2724  freerdp_client_file_string_check_free(file->DrivesToRedirect);
2725  freerdp_client_file_string_check_free(file->DevicesToRedirect);
2726  freerdp_client_file_string_check_free(file->WinPosStr);
2727  freerdp_client_file_string_check_free(file->ResourceProvider);
2728  freerdp_client_file_string_check_free(file->WvdEndpointPool);
2729  freerdp_client_file_string_check_free(file->geo);
2730  freerdp_client_file_string_check_free(file->armpath);
2731  freerdp_client_file_string_check_free(file->aadtenantid);
2732  freerdp_client_file_string_check_free(file->diagnosticserviceurl);
2733  freerdp_client_file_string_check_free(file->hubdiscoverygeourl);
2734  freerdp_client_file_string_check_free(file->activityhint);
2735  free(file);
2736  }
2737 }
2738 
2739 void freerdp_client_rdp_file_set_callback_context(rdpFile* file, void* context)
2740 {
2741  file->context = context;
2742 }
#define LOCATION_CHANNEL_NAME
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
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 BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
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 BOOL freerdp_set_gateway_usage_method(rdpSettings *settings, UINT32 GatewayUsageMethod)
FREERDP_API WCHAR * freerdp_settings_get_string_as_utf16(const rdpSettings *settings, FreeRDP_Settings_Keys_String id, size_t *pCharLen)
Return an allocated UTF16 string.
FREERDP_API BOOL freerdp_settings_set_string_from_utf16(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const WCHAR *param)
Sets a string settings value. The param is converted to UTF-8 and the copy stored.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.
FREERDP_API void * freerdp_settings_get_pointer_writable(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a mutable pointer settings value.