Coverage Report

Created: 2026-02-26 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/client/common/file.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * .rdp file
4
 *
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
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
38
/**
39
 * Remote Desktop Plus - Overview of .rdp file settings:
40
 * http://www.donkz.nl/files/rdpsettings.html
41
 *
42
 * RDP Settings for Remote Desktop Services in Windows Server 2008 R2:
43
 * http://technet.microsoft.com/en-us/library/ff393699/
44
 *
45
 * https://docs.microsoft.com/en-us/windows-server/remote/remote-desktop-services/clients/rdp-files
46
 */
47
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
0
#define RDP_FILE_LINE_FLAG_FORMATTED 0x00000001
64
// #define RDP_FILE_LINE_FLAG_STANDARD 0x00000002
65
0
#define RDP_FILE_LINE_FLAG_TYPE_STRING 0x00000010
66
0
#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
0
{
342
0
  if (!str)
343
0
    return TRUE;
344
0
  if (strlen(str) == 0)
345
0
    return TRUE;
346
0
  return FALSE;
347
0
}
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
0
{
357
0
  WINPR_ASSERT(file);
358
0
  WINPR_ASSERT(name);
359
0
  WINPR_ASSERT(outValue);
360
0
  WINPR_ASSERT(outLine);
361
362
0
  *outValue = NULL;
363
0
  *outLine = NULL;
364
365
0
  if (_stricmp(name, key_int_use_multimon) == 0)
366
0
    *outValue = &file->UseMultiMon;
367
0
  else if (_stricmp(name, key_int_maximizetocurrentdisplays) == 0)
368
0
    *outValue = &file->MaximizeToCurrentDisplays;
369
0
  else if (_stricmp(name, key_int_singlemoninwindowedmode) == 0)
370
0
    *outValue = &file->SingleMonInWindowedMode;
371
0
  else if (_stricmp(name, key_int_screen_mode_id) == 0)
372
0
    *outValue = &file->ScreenModeId;
373
0
  else if (_stricmp(name, key_int_span_monitors) == 0)
374
0
    *outValue = &file->SpanMonitors;
375
0
  else if (_stricmp(name, key_int_smart_sizing) == 0)
376
0
    *outValue = &file->SmartSizing;
377
0
  else if (_stricmp(name, key_int_dynamic_resolution) == 0)
378
0
    *outValue = &file->DynamicResolution;
379
0
  else if (_stricmp(name, key_int_enablesuperpan) == 0)
380
0
    *outValue = &file->EnableSuperSpan;
381
0
  else if (_stricmp(name, key_int_superpanaccelerationfactor) == 0)
382
0
    *outValue = &file->SuperSpanAccelerationFactor;
383
0
  else if (_stricmp(name, key_int_desktopwidth) == 0)
384
0
    *outValue = &file->DesktopWidth;
385
0
  else if (_stricmp(name, key_int_desktopheight) == 0)
386
0
    *outValue = &file->DesktopHeight;
387
0
  else if (_stricmp(name, key_int_desktop_size_id) == 0)
388
0
    *outValue = &file->DesktopSizeId;
389
0
  else if (_stricmp(name, key_int_session_bpp) == 0)
390
0
    *outValue = &file->SessionBpp;
391
0
  else if (_stricmp(name, key_int_desktopscalefactor) == 0)
392
0
    *outValue = &file->DesktopScaleFactor;
393
0
  else if (_stricmp(name, key_int_compression) == 0)
394
0
    *outValue = &file->Compression;
395
0
  else if (_stricmp(name, key_int_keyboardhook) == 0)
396
0
    *outValue = &file->KeyboardHook;
397
0
  else if (_stricmp(name, key_int_disable_ctrl_alt_del) == 0)
398
0
    *outValue = &file->DisableCtrlAltDel;
399
0
  else if (_stricmp(name, key_int_audiomode) == 0)
400
0
    *outValue = &file->AudioMode;
401
0
  else if (_stricmp(name, key_int_audioqualitymode) == 0)
402
0
    *outValue = &file->AudioQualityMode;
403
0
  else if (_stricmp(name, key_int_audiocapturemode) == 0)
404
0
    *outValue = &file->AudioCaptureMode;
405
0
  else if (_stricmp(name, key_int_encode_redirected_video_capture) == 0)
406
0
    *outValue = &file->EncodeRedirectedVideoCapture;
407
0
  else if (_stricmp(name, key_int_redirected_video_capture_encoding_quality) == 0)
408
0
    *outValue = &file->RedirectedVideoCaptureEncodingQuality;
409
0
  else if (_stricmp(name, key_int_videoplaybackmode) == 0)
410
0
    *outValue = &file->VideoPlaybackMode;
411
0
  else if (_stricmp(name, key_int_connection_type) == 0)
412
0
    *outValue = &file->ConnectionType;
413
0
  else if (_stricmp(name, key_int_networkautodetect) == 0)
414
0
    *outValue = &file->NetworkAutoDetect;
415
0
  else if (_stricmp(name, key_int_bandwidthautodetect) == 0)
416
0
    *outValue = &file->BandwidthAutoDetect;
417
0
  else if (_stricmp(name, key_int_pinconnectionbar) == 0)
418
0
    *outValue = &file->PinConnectionBar;
419
0
  else if (_stricmp(name, key_int_displayconnectionbar) == 0)
420
0
    *outValue = &file->DisplayConnectionBar;
421
0
  else if (_stricmp(name, key_int_workspaceid) == 0)
422
0
    *outValue = &file->WorkspaceId;
423
0
  else if (_stricmp(name, key_int_enableworkspacereconnect) == 0)
424
0
    *outValue = &file->EnableWorkspaceReconnect;
425
0
  else if (_stricmp(name, key_int_disable_wallpaper) == 0)
426
0
    *outValue = &file->DisableWallpaper;
427
0
  else if (_stricmp(name, key_int_allow_font_smoothing) == 0)
428
0
    *outValue = &file->AllowFontSmoothing;
429
0
  else if (_stricmp(name, key_int_allow_desktop_composition) == 0)
430
0
    *outValue = &file->AllowDesktopComposition;
431
0
  else if (_stricmp(name, key_int_disable_full_window_drag) == 0)
432
0
    *outValue = &file->DisableFullWindowDrag;
433
0
  else if (_stricmp(name, key_int_disable_menu_anims) == 0)
434
0
    *outValue = &file->DisableMenuAnims;
435
0
  else if (_stricmp(name, key_int_disable_themes) == 0)
436
0
    *outValue = &file->DisableThemes;
437
0
  else if (_stricmp(name, key_int_disable_cursor_setting) == 0)
438
0
    *outValue = &file->DisableCursorSetting;
439
0
  else if (_stricmp(name, key_int_bitmapcachesize) == 0)
440
0
    *outValue = &file->BitmapCacheSize;
441
0
  else if (_stricmp(name, key_int_bitmapcachepersistenable) == 0)
442
0
    *outValue = &file->BitmapCachePersistEnable;
443
0
  else if (_stricmp(name, key_int_server_port) == 0)
444
0
    *outValue = &file->ServerPort;
445
0
  else if (_stricmp(name, key_int_redirectdrives) == 0)
446
0
    *outValue = &file->RedirectDrives;
447
0
  else if (_stricmp(name, key_int_redirectprinters) == 0)
448
0
    *outValue = &file->RedirectPrinters;
449
0
  else if (_stricmp(name, key_int_redirectcomports) == 0)
450
0
    *outValue = &file->RedirectComPorts;
451
0
  else if (_stricmp(name, key_int_redirectlocation) == 0)
452
0
    *outValue = &file->RedirectLocation;
453
0
  else if (_stricmp(name, key_int_redirectsmartcards) == 0)
454
0
    *outValue = &file->RedirectSmartCards;
455
0
  else if (_stricmp(name, key_int_redirectclipboard) == 0)
456
0
    *outValue = &file->RedirectClipboard;
457
0
  else if (_stricmp(name, key_int_redirectposdevices) == 0)
458
0
    *outValue = &file->RedirectPosDevices;
459
0
  else if (_stricmp(name, key_int_redirectdirectx) == 0)
460
0
    *outValue = &file->RedirectDirectX;
461
0
  else if (_stricmp(name, key_int_disableprinterredirection) == 0)
462
0
    *outValue = &file->DisablePrinterRedirection;
463
0
  else if (_stricmp(name, key_int_disableclipboardredirection) == 0)
464
0
    *outValue = &file->DisableClipboardRedirection;
465
0
  else if (_stricmp(name, key_int_connect_to_console) == 0)
466
0
    *outValue = &file->ConnectToConsole;
467
0
  else if (_stricmp(name, key_int_administrative_session) == 0)
468
0
    *outValue = &file->AdministrativeSession;
469
0
  else if (_stricmp(name, key_int_autoreconnection_enabled) == 0)
470
0
    *outValue = &file->AutoReconnectionEnabled;
471
0
  else if (_stricmp(name, key_int_autoreconnect_max_retries) == 0)
472
0
    *outValue = &file->AutoReconnectMaxRetries;
473
0
  else if (_stricmp(name, key_int_public_mode) == 0)
474
0
    *outValue = &file->PublicMode;
475
0
  else if (_stricmp(name, key_int_authentication_level) == 0)
476
0
    *outValue = &file->AuthenticationLevel;
477
0
  else if (_stricmp(name, key_int_promptcredentialonce) == 0)
478
0
    *outValue = &file->PromptCredentialOnce;
479
0
  else if ((_stricmp(name, key_int_prompt_for_credentials) == 0))
480
0
    *outValue = &file->PromptForCredentials;
481
0
  else if (_stricmp(name, key_int_negotiate_security_layer) == 0)
482
0
    *outValue = &file->NegotiateSecurityLayer;
483
0
  else if (_stricmp(name, key_int_enablecredsspsupport) == 0)
484
0
    *outValue = &file->EnableCredSSPSupport;
485
0
  else if (_stricmp(name, key_int_enablerdsaadauth) == 0)
486
0
    *outValue = &file->EnableRdsAadAuth;
487
0
  else if (_stricmp(name, key_int_remoteapplicationmode) == 0)
488
0
    *outValue = &file->RemoteApplicationMode;
489
0
  else if (_stricmp(name, key_int_remoteapplicationexpandcmdline) == 0)
490
0
    *outValue = &file->RemoteApplicationExpandCmdLine;
491
0
  else if (_stricmp(name, key_int_remoteapplicationexpandworkingdir) == 0)
492
0
    *outValue = &file->RemoteApplicationExpandWorkingDir;
493
0
  else if (_stricmp(name, key_int_disableconnectionsharing) == 0)
494
0
    *outValue = &file->DisableConnectionSharing;
495
0
  else if (_stricmp(name, key_int_disableremoteappcapscheck) == 0)
496
0
    *outValue = &file->DisableRemoteAppCapsCheck;
497
0
  else if (_stricmp(name, key_int_gatewayusagemethod) == 0)
498
0
    *outValue = &file->GatewayUsageMethod;
499
0
  else if (_stricmp(name, key_int_gatewayprofileusagemethod) == 0)
500
0
    *outValue = &file->GatewayProfileUsageMethod;
501
0
  else if (_stricmp(name, key_int_gatewaycredentialssource) == 0)
502
0
    *outValue = &file->GatewayCredentialsSource;
503
0
  else if (_stricmp(name, key_int_use_redirection_server_name) == 0)
504
0
    *outValue = &file->UseRedirectionServerName;
505
0
  else if (_stricmp(name, key_int_rdgiskdcproxy) == 0)
506
0
    *outValue = &file->RdgIsKdcProxy;
507
0
  else if (_stricmp(name, key_int_redirectwebauthn) == 0)
508
0
    *outValue = &file->RedirectWebauthN;
509
0
  else
510
0
  {
511
0
    rdpFileLine* line = freerdp_client_rdp_file_find_line_by_name(file, name);
512
0
    if (!line)
513
0
      return FALSE;
514
0
    if (!(line->flags & RDP_FILE_LINE_FLAG_TYPE_INTEGER))
515
0
      return FALSE;
516
517
0
    *outLine = line;
518
0
  }
519
520
0
  return TRUE;
521
0
}
522
523
static BOOL freerdp_client_rdp_file_find_string_entry(rdpFile* file, const char* name,
524
                                                      LPSTR** outValue, rdpFileLine** outLine)
525
0
{
526
0
  WINPR_ASSERT(file);
527
0
  WINPR_ASSERT(name);
528
0
  WINPR_ASSERT(outValue);
529
0
  WINPR_ASSERT(outLine);
530
531
0
  *outValue = NULL;
532
0
  *outLine = NULL;
533
534
0
  if (_stricmp(name, key_str_username) == 0)
535
0
    *outValue = &file->Username;
536
0
  else if (_stricmp(name, key_str_domain) == 0)
537
0
    *outValue = &file->Domain;
538
0
  else if (_stricmp(name, key_str_password) == 0)
539
0
    *outValue = &file->Password;
540
0
  else if (_stricmp(name, key_str_full_address) == 0)
541
0
    *outValue = &file->FullAddress;
542
0
  else if (_stricmp(name, key_str_alternate_full_address) == 0)
543
0
    *outValue = &file->AlternateFullAddress;
544
0
  else if (_stricmp(name, key_str_usbdevicestoredirect) == 0)
545
0
    *outValue = &file->UsbDevicesToRedirect;
546
0
  else if (_stricmp(name, key_str_camerastoredirect) == 0)
547
0
    *outValue = &file->RedirectCameras;
548
0
  else if (_stricmp(name, key_str_loadbalanceinfo) == 0)
549
0
    *outValue = &file->LoadBalanceInfo;
550
0
  else if (_stricmp(name, key_str_remoteapplicationname) == 0)
551
0
    *outValue = &file->RemoteApplicationName;
552
0
  else if (_stricmp(name, key_str_remoteapplicationicon) == 0)
553
0
    *outValue = &file->RemoteApplicationIcon;
554
0
  else if (_stricmp(name, key_str_remoteapplicationprogram) == 0)
555
0
    *outValue = &file->RemoteApplicationProgram;
556
0
  else if (_stricmp(name, key_str_remoteapplicationfile) == 0)
557
0
    *outValue = &file->RemoteApplicationFile;
558
0
  else if (_stricmp(name, key_str_remoteapplicationguid) == 0)
559
0
    *outValue = &file->RemoteApplicationGuid;
560
0
  else if (_stricmp(name, key_str_remoteapplicationcmdline) == 0)
561
0
    *outValue = &file->RemoteApplicationCmdLine;
562
0
  else if (_stricmp(name, key_str_alternate_shell) == 0)
563
0
    *outValue = &file->AlternateShell;
564
0
  else if (_stricmp(name, key_str_shell_working_directory) == 0)
565
0
    *outValue = &file->ShellWorkingDirectory;
566
0
  else if (_stricmp(name, key_str_gatewayhostname) == 0)
567
0
    *outValue = &file->GatewayHostname;
568
0
  else if (_stricmp(name, key_str_resourceprovider) == 0)
569
0
    *outValue = &file->ResourceProvider;
570
0
  else if (_stricmp(name, key_str_wvd) == 0)
571
0
    *outValue = &file->WvdEndpointPool;
572
0
  else if (_stricmp(name, key_str_geo) == 0)
573
0
    *outValue = &file->geo;
574
0
  else if (_stricmp(name, key_str_armpath) == 0)
575
0
    *outValue = &file->armpath;
576
0
  else if (_stricmp(name, key_str_aadtenantid) == 0)
577
0
    *outValue = &file->aadtenantid;
578
0
  else if (_stricmp(name, key_str_diagnosticserviceurl) == 0)
579
0
    *outValue = &file->diagnosticserviceurl;
580
0
  else if (_stricmp(name, key_str_hubdiscoverygeourl) == 0)
581
0
    *outValue = &file->hubdiscoverygeourl;
582
0
  else if (_stricmp(name, key_str_activityhint) == 0)
583
0
    *outValue = &file->activityhint;
584
0
  else if (_stricmp(name, key_str_gatewayaccesstoken) == 0)
585
0
    *outValue = &file->GatewayAccessToken;
586
0
  else if (_stricmp(name, key_str_kdcproxyname) == 0)
587
0
    *outValue = &file->KdcProxyName;
588
0
  else if (_stricmp(name, key_str_drivestoredirect) == 0)
589
0
    *outValue = &file->DrivesToRedirect;
590
0
  else if (_stricmp(name, key_str_devicestoredirect) == 0)
591
0
    *outValue = &file->DevicesToRedirect;
592
0
  else if (_stricmp(name, key_str_winposstr) == 0)
593
0
    *outValue = &file->WinPosStr;
594
0
  else if (_stricmp(name, key_str_pcb) == 0)
595
0
    *outValue = &file->PreconnectionBlob;
596
0
  else if (_stricmp(name, key_str_selectedmonitors) == 0)
597
0
    *outValue = &file->SelectedMonitors;
598
0
  else
599
0
  {
600
0
    rdpFileLine* line = freerdp_client_rdp_file_find_line_by_name(file, name);
601
0
    if (!line)
602
0
      return FALSE;
603
0
    if (!(line->flags & RDP_FILE_LINE_FLAG_TYPE_STRING))
604
0
      return FALSE;
605
606
0
    *outLine = line;
607
0
  }
608
609
0
  return TRUE;
610
0
}
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
0
{
620
0
  DWORD* targetValue = NULL;
621
0
  rdpFileLine* line = NULL;
622
#ifdef DEBUG_CLIENT_FILE
623
  WLog_DBG(TAG, "%s:i:%ld", name, value);
624
#endif
625
626
0
  if (value < 0)
627
0
    return FALSE;
628
629
0
  if (!freerdp_client_rdp_file_find_integer_entry(file, name, &targetValue, &line))
630
0
  {
631
0
    SSIZE_T index = freerdp_client_rdp_file_add_line(file);
632
0
    if (index == -1)
633
0
      return FALSE;
634
0
    line = &file->lines[index];
635
0
  }
636
637
0
  if (targetValue)
638
0
  {
639
0
    *targetValue = (DWORD)value;
640
0
    return TRUE;
641
0
  }
642
643
0
  if (line)
644
0
  {
645
0
    free(line->name);
646
0
    line->name = _strdup(name);
647
0
    if (!line->name)
648
0
    {
649
0
      free(line->name);
650
0
      line->name = NULL;
651
0
      return FALSE;
652
0
    }
653
654
0
    line->iValue = value;
655
0
    line->flags = RDP_FILE_LINE_FLAG_FORMATTED;
656
0
    line->flags |= RDP_FILE_LINE_FLAG_TYPE_INTEGER;
657
0
    line->valueLength = 0;
658
0
    return TRUE;
659
0
  }
660
661
0
  return FALSE;
662
0
}
663
664
static BOOL freerdp_client_parse_rdp_file_integer(rdpFile* file, const char* name,
665
                                                  const char* value)
666
0
{
667
0
  char* endptr = NULL;
668
0
  long ivalue = 0;
669
0
  errno = 0;
670
0
  ivalue = strtol(value, &endptr, 0);
671
672
0
  if ((endptr == NULL) || (errno != 0) || (endptr == value) || (ivalue > INT32_MAX) ||
673
0
      (ivalue < INT32_MIN))
674
0
  {
675
0
    if (file->flags & RDP_FILE_FLAG_PARSE_INT_RELAXED)
676
0
    {
677
0
      WLog_WARN(TAG, "Integer option %s has invalid value %s, using default", name, value);
678
0
      return TRUE;
679
0
    }
680
0
    else
681
0
    {
682
0
      WLog_ERR(TAG, "Failed to convert RDP file integer option %s [value=%s]", name, value);
683
0
      return FALSE;
684
0
    }
685
0
  }
686
687
0
  return freerdp_client_rdp_file_set_integer(file, name, ivalue);
688
0
}
689
690
/** set a string value in the provided rdp file context
691
 *
692
 * @param file rdpFile
693
 * @param name name of the string
694
 * @param value value of the string to set
695
 * @return 0 on success, 1 if the key wasn't found (not a standard key), -1 on error
696
 */
697
698
static BOOL freerdp_client_rdp_file_set_string(rdpFile* file, const char* name, const char* value)
699
0
{
700
0
  LPSTR* targetValue = NULL;
701
0
  rdpFileLine* line = NULL;
702
#ifdef DEBUG_CLIENT_FILE
703
  WLog_DBG(TAG, "%s:s:%s", name, value);
704
#endif
705
706
0
  if (!name || !value)
707
0
    return FALSE;
708
709
0
  if (!freerdp_client_rdp_file_find_string_entry(file, name, &targetValue, &line))
710
0
  {
711
0
    SSIZE_T index = freerdp_client_rdp_file_add_line(file);
712
0
    if (index == -1)
713
0
      return FALSE;
714
0
    line = &file->lines[index];
715
0
  }
716
717
0
  if (targetValue)
718
0
  {
719
0
    if ((uintptr_t)(*targetValue) != UINTPTR_MAX)
720
0
      free(*targetValue);
721
722
0
    *targetValue = _strdup(value);
723
0
    if (!(*targetValue))
724
0
      return FALSE;
725
0
    return TRUE;
726
0
  }
727
728
0
  if (line)
729
0
  {
730
0
    free(line->name);
731
0
    free(line->sValue);
732
0
    line->name = _strdup(name);
733
0
    line->sValue = _strdup(value);
734
0
    if (!line->name || !line->sValue)
735
0
    {
736
0
      free(line->name);
737
0
      free(line->sValue);
738
0
      line->name = NULL;
739
0
      line->sValue = NULL;
740
0
      return FALSE;
741
0
    }
742
743
0
    line->flags = RDP_FILE_LINE_FLAG_FORMATTED;
744
0
    line->flags |= RDP_FILE_LINE_FLAG_TYPE_STRING;
745
0
    line->valueLength = 0;
746
0
    return TRUE;
747
0
  }
748
749
0
  return FALSE;
750
0
}
751
752
static BOOL freerdp_client_add_option(rdpFile* file, const char* option)
753
0
{
754
0
  return freerdp_addin_argv_add_argument(file->args, option);
755
0
}
756
757
static SSIZE_T freerdp_client_rdp_file_add_line(rdpFile* file)
758
0
{
759
0
  SSIZE_T index = (SSIZE_T)file->lineCount;
760
761
0
  while ((file->lineCount + 1) > file->lineSize)
762
0
  {
763
0
    size_t new_size = file->lineCount + 2048;
764
0
    rdpFileLine* new_line = (rdpFileLine*)realloc(file->lines, new_size * sizeof(rdpFileLine));
765
766
0
    if (!new_line)
767
0
      return -1;
768
769
0
    file->lines = new_line;
770
0
    file->lineSize = new_size;
771
0
  }
772
773
0
  ZeroMemory(&(file->lines[file->lineCount]), sizeof(rdpFileLine));
774
0
  file->lines[file->lineCount].index = (size_t)index;
775
0
  (file->lineCount)++;
776
0
  return index;
777
0
}
778
779
static BOOL freerdp_client_parse_rdp_file_string(rdpFile* file, char* name, char* value)
780
0
{
781
0
  return freerdp_client_rdp_file_set_string(file, name, value);
782
0
}
783
784
static BOOL freerdp_client_parse_rdp_file_option(rdpFile* file, const char* option)
785
0
{
786
0
  return freerdp_client_add_option(file, option);
787
0
}
788
789
BOOL freerdp_client_parse_rdp_file_buffer(rdpFile* file, const BYTE* buffer, size_t size)
790
0
{
791
0
  return freerdp_client_parse_rdp_file_buffer_ex(file, buffer, size, NULL);
792
0
}
793
794
static BOOL trim(char** strptr)
795
0
{
796
0
  char* start = NULL;
797
0
  char* str = NULL;
798
0
  char* end = NULL;
799
800
0
  start = str = *strptr;
801
0
  if (!str)
802
0
    return TRUE;
803
0
  if (!(~((size_t)str)))
804
0
    return TRUE;
805
0
  end = str + strlen(str) - 1;
806
807
0
  while (isspace(*str))
808
0
    str++;
809
810
0
  while ((end > str) && isspace(*end))
811
0
    end--;
812
0
  end[1] = '\0';
813
0
  if (start == str)
814
0
    *strptr = str;
815
0
  else
816
0
  {
817
0
    *strptr = _strdup(str);
818
0
    free(start);
819
0
    return *strptr != NULL;
820
0
  }
821
822
0
  return TRUE;
823
0
}
824
825
static BOOL trim_strings(rdpFile* file)
826
0
{
827
0
  if (!trim(&file->Username))
828
0
    return FALSE;
829
0
  if (!trim(&file->Domain))
830
0
    return FALSE;
831
0
  if (!trim(&file->AlternateFullAddress))
832
0
    return FALSE;
833
0
  if (!trim(&file->FullAddress))
834
0
    return FALSE;
835
0
  if (!trim(&file->UsbDevicesToRedirect))
836
0
    return FALSE;
837
0
  if (!trim(&file->RedirectCameras))
838
0
    return FALSE;
839
0
  if (!trim(&file->LoadBalanceInfo))
840
0
    return FALSE;
841
0
  if (!trim(&file->GatewayHostname))
842
0
    return FALSE;
843
0
  if (!trim(&file->GatewayAccessToken))
844
0
    return FALSE;
845
0
  if (!trim(&file->RemoteApplicationName))
846
0
    return FALSE;
847
0
  if (!trim(&file->RemoteApplicationIcon))
848
0
    return FALSE;
849
0
  if (!trim(&file->RemoteApplicationProgram))
850
0
    return FALSE;
851
0
  if (!trim(&file->RemoteApplicationFile))
852
0
    return FALSE;
853
0
  if (!trim(&file->RemoteApplicationGuid))
854
0
    return FALSE;
855
0
  if (!trim(&file->RemoteApplicationCmdLine))
856
0
    return FALSE;
857
0
  if (!trim(&file->AlternateShell))
858
0
    return FALSE;
859
0
  if (!trim(&file->ShellWorkingDirectory))
860
0
    return FALSE;
861
0
  if (!trim(&file->DrivesToRedirect))
862
0
    return FALSE;
863
0
  if (!trim(&file->DevicesToRedirect))
864
0
    return FALSE;
865
0
  if (!trim(&file->DevicesToRedirect))
866
0
    return FALSE;
867
0
  if (!trim(&file->WinPosStr))
868
0
    return FALSE;
869
0
  if (!trim(&file->PreconnectionBlob))
870
0
    return FALSE;
871
0
  if (!trim(&file->KdcProxyName))
872
0
    return FALSE;
873
0
  if (!trim(&file->SelectedMonitors))
874
0
    return FALSE;
875
876
0
  for (size_t i = 0; i < file->lineCount; ++i)
877
0
  {
878
0
    rdpFileLine* curLine = &file->lines[i];
879
0
    if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_STRING)
880
0
    {
881
0
      if (!trim(&curLine->sValue))
882
0
        return FALSE;
883
0
    }
884
0
  }
885
886
0
  return TRUE;
887
0
}
888
889
BOOL freerdp_client_parse_rdp_file_buffer_ex(rdpFile* file, const BYTE* buffer, size_t size,
890
                                             rdp_file_fkt_parse parse)
891
0
{
892
0
  BOOL rc = FALSE;
893
0
  size_t length = 0;
894
0
  char* line = NULL;
895
0
  char* type = NULL;
896
0
  char* context = NULL;
897
0
  char* d1 = NULL;
898
0
  char* d2 = NULL;
899
0
  char* beg = NULL;
900
0
  char* name = NULL;
901
0
  char* value = NULL;
902
0
  char* copy = NULL;
903
904
0
  if (!file)
905
0
    return FALSE;
906
0
  if (size < 2)
907
0
    return FALSE;
908
909
0
  if ((buffer[0] == BOM_UTF16_LE[0]) && (buffer[1] == BOM_UTF16_LE[1]))
910
0
  {
911
0
    LPCWSTR uc = (LPCWSTR)(&buffer[2]);
912
0
    size = size / sizeof(WCHAR) - 1;
913
914
0
    copy = ConvertWCharNToUtf8Alloc(uc, size, NULL);
915
0
    if (!copy)
916
0
    {
917
0
      WLog_ERR(TAG, "Failed to convert RDP file from UCS2 to UTF8");
918
0
      return FALSE;
919
0
    }
920
0
  }
921
0
  else
922
0
  {
923
0
    copy = calloc(1, size + sizeof(BYTE));
924
925
0
    if (!copy)
926
0
      return FALSE;
927
928
0
    memcpy(copy, buffer, size);
929
0
  }
930
931
0
  line = strtok_s(copy, "\r\n", &context);
932
933
0
  while (line)
934
0
  {
935
0
    length = strnlen(line, size);
936
937
0
    if (length > 1)
938
0
    {
939
0
      beg = line;
940
0
      if (beg[0] == '/')
941
0
      {
942
0
        if (!freerdp_client_parse_rdp_file_option(file, line))
943
0
          goto fail;
944
945
0
        goto next_line; /* FreeRDP option */
946
0
      }
947
948
0
      d1 = strchr(line, ':');
949
950
0
      if (!d1)
951
0
        goto next_line; /* not first delimiter */
952
953
0
      type = &d1[1];
954
0
      d2 = strchr(type, ':');
955
956
0
      if (!d2)
957
0
        goto next_line; /* no second delimiter */
958
959
0
      if ((d2 - d1) != 2)
960
0
        goto next_line; /* improper type length */
961
962
0
      *d1 = 0;
963
0
      *d2 = 0;
964
0
      name = beg;
965
0
      value = &d2[1];
966
967
0
      if (parse && parse(file->context, name, *type, value))
968
0
      {
969
0
      }
970
0
      else if (*type == 'i')
971
0
      {
972
        /* integer type */
973
0
        if (!freerdp_client_parse_rdp_file_integer(file, name, value))
974
0
          goto fail;
975
0
      }
976
0
      else if (*type == 's')
977
0
      {
978
        /* string type */
979
0
        if (!freerdp_client_parse_rdp_file_string(file, name, value))
980
0
          goto fail;
981
0
      }
982
0
      else if (*type == 'b')
983
0
      {
984
        /* binary type */
985
0
        WLog_ERR(TAG, "Unsupported RDP file binary option %s [value=%s]", name, value);
986
0
      }
987
0
    }
988
989
0
  next_line:
990
0
    line = strtok_s(NULL, "\r\n", &context);
991
0
  }
992
993
0
  rc = trim_strings(file);
994
0
fail:
995
0
  free(copy);
996
0
  return rc;
997
0
}
998
999
BOOL freerdp_client_parse_rdp_file(rdpFile* file, const char* name)
1000
0
{
1001
0
  return freerdp_client_parse_rdp_file_ex(file, name, NULL);
1002
0
}
1003
1004
BOOL freerdp_client_parse_rdp_file_ex(rdpFile* file, const char* name, rdp_file_fkt_parse parse)
1005
0
{
1006
0
  BOOL status = 0;
1007
0
  BYTE* buffer = NULL;
1008
0
  FILE* fp = NULL;
1009
0
  size_t read_size = 0;
1010
0
  INT64 file_size = 0;
1011
0
  const char* fname = name;
1012
1013
0
  if (!file || !name)
1014
0
    return FALSE;
1015
1016
0
  if (_strnicmp(fname, "file://", 7) == 0)
1017
0
    fname = &name[7];
1018
1019
0
  fp = winpr_fopen(fname, "r");
1020
0
  if (!fp)
1021
0
  {
1022
0
    WLog_ERR(TAG, "Failed to open RDP file %s", name);
1023
0
    return FALSE;
1024
0
  }
1025
1026
0
  (void)_fseeki64(fp, 0, SEEK_END);
1027
0
  file_size = _ftelli64(fp);
1028
0
  (void)_fseeki64(fp, 0, SEEK_SET);
1029
1030
0
  if (file_size < 1)
1031
0
  {
1032
0
    WLog_ERR(TAG, "RDP file %s is empty", name);
1033
0
    (void)fclose(fp);
1034
0
    return FALSE;
1035
0
  }
1036
1037
0
  buffer = (BYTE*)malloc((size_t)file_size + 2);
1038
1039
0
  if (!buffer)
1040
0
  {
1041
0
    (void)fclose(fp);
1042
0
    return FALSE;
1043
0
  }
1044
1045
0
  read_size = fread(buffer, (size_t)file_size, 1, fp);
1046
1047
0
  if (!read_size)
1048
0
  {
1049
0
    if (!ferror(fp))
1050
0
      read_size = (size_t)file_size;
1051
0
  }
1052
1053
0
  (void)fclose(fp);
1054
1055
0
  if (read_size < 1)
1056
0
  {
1057
0
    WLog_ERR(TAG, "Could not read from RDP file %s", name);
1058
0
    free(buffer);
1059
0
    return FALSE;
1060
0
  }
1061
1062
0
  buffer[file_size] = '\0';
1063
0
  buffer[file_size + 1] = '\0';
1064
0
  status = freerdp_client_parse_rdp_file_buffer_ex(file, buffer, (size_t)file_size, parse);
1065
0
  free(buffer);
1066
0
  return status;
1067
0
}
1068
1069
static inline void freerdp_client_file_string_reset(char** target)
1070
0
{
1071
0
  WINPR_ASSERT(target);
1072
0
  freerdp_client_file_string_check_free(*target);
1073
0
  *target = (void*)~((size_t)NULL);
1074
0
}
1075
1076
static inline BOOL FILE_POPULATE_STRING(char** _target, const rdpSettings* _settings,
1077
                                        FreeRDP_Settings_Keys_String _option)
1078
0
{
1079
0
  WINPR_ASSERT(_target);
1080
0
  WINPR_ASSERT(_settings);
1081
1082
0
  const char* str = freerdp_settings_get_string(_settings, _option);
1083
0
  freerdp_client_file_string_reset(_target);
1084
0
  if (str)
1085
0
  {
1086
0
    char* copy = _strdup(str);
1087
0
    if (!copy)
1088
0
      return FALSE;
1089
0
    *_target = copy;
1090
0
  }
1091
0
  return TRUE;
1092
0
}
1093
1094
static char* freerdp_client_channel_args_to_string(const rdpSettings* settings, const char* channel,
1095
                                                   const char* option)
1096
0
{
1097
0
  ADDIN_ARGV* args = freerdp_dynamic_channel_collection_find(settings, channel);
1098
0
  const char* filters[] = { option };
1099
0
  if (!args || (args->argc < 2))
1100
0
    return NULL;
1101
1102
0
  return CommandLineToCommaSeparatedValuesEx(args->argc - 1, args->argv + 1, filters,
1103
0
                                             ARRAYSIZE(filters));
1104
0
}
1105
1106
static BOOL rdp_opt_duplicate(const rdpSettings* _settings, FreeRDP_Settings_Keys_String _id,
1107
                              char** _key)
1108
0
{
1109
0
  WINPR_ASSERT(_settings);
1110
0
  WINPR_ASSERT(_key);
1111
0
  const char* tmp = freerdp_settings_get_string(_settings, _id);
1112
1113
0
  if (tmp)
1114
0
  {
1115
0
    *_key = _strdup(tmp);
1116
0
    if (!*_key)
1117
0
      return FALSE;
1118
0
  }
1119
1120
0
  return TRUE;
1121
0
}
1122
1123
BOOL freerdp_client_populate_rdp_file_from_settings(rdpFile* file, const rdpSettings* settings)
1124
0
{
1125
0
  FreeRDP_Settings_Keys_String index = FreeRDP_STRING_UNUSED;
1126
0
  UINT32 LoadBalanceInfoLength = 0;
1127
0
  const char* GatewayHostname = NULL;
1128
0
  char* redirectCameras = NULL;
1129
1130
0
  if (!file || !settings)
1131
0
    return FALSE;
1132
1133
0
  if (!FILE_POPULATE_STRING(&file->Domain, settings, FreeRDP_Domain) ||
1134
0
      !FILE_POPULATE_STRING(&file->Username, settings, FreeRDP_Username) ||
1135
0
      !FILE_POPULATE_STRING(&file->Password, settings, FreeRDP_Password) ||
1136
0
      !FILE_POPULATE_STRING(&file->FullAddress, settings, FreeRDP_ServerHostname) ||
1137
0
      !FILE_POPULATE_STRING(&file->AlternateFullAddress, settings, FreeRDP_ServerHostname) ||
1138
0
      !FILE_POPULATE_STRING(&file->AlternateShell, settings, FreeRDP_AlternateShell) ||
1139
0
      !FILE_POPULATE_STRING(&file->DrivesToRedirect, settings, FreeRDP_DrivesToRedirect))
1140
1141
0
    return FALSE;
1142
0
  file->ServerPort = freerdp_settings_get_uint32(settings, FreeRDP_ServerPort);
1143
1144
0
  file->DesktopWidth = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
1145
0
  file->DesktopHeight = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
1146
0
  file->SessionBpp = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
1147
0
  file->DesktopScaleFactor = freerdp_settings_get_uint32(settings, FreeRDP_DesktopScaleFactor);
1148
0
  file->DynamicResolution = WINPR_ASSERTING_INT_CAST(
1149
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_SupportDisplayControl));
1150
0
  file->VideoPlaybackMode = WINPR_ASSERTING_INT_CAST(
1151
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_SupportVideoOptimized));
1152
1153
  // TODO file->MaximizeToCurrentDisplays;
1154
  // TODO file->SingleMonInWindowedMode;
1155
  // TODO file->EncodeRedirectedVideoCapture;
1156
  // TODO file->RedirectedVideoCaptureEncodingQuality;
1157
0
  file->ConnectToConsole = WINPR_ASSERTING_INT_CAST(
1158
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_ConsoleSession));
1159
0
  file->NegotiateSecurityLayer = WINPR_ASSERTING_INT_CAST(
1160
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_NegotiateSecurityLayer));
1161
0
  file->EnableCredSSPSupport =
1162
0
      WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_NlaSecurity));
1163
0
  file->EnableRdsAadAuth =
1164
0
      WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_AadSecurity));
1165
1166
0
  if (freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode))
1167
0
    index = FreeRDP_RemoteApplicationWorkingDir;
1168
0
  else
1169
0
    index = FreeRDP_ShellWorkingDirectory;
1170
0
  if (!FILE_POPULATE_STRING(&file->ShellWorkingDirectory, settings, index))
1171
0
    return FALSE;
1172
0
  file->ConnectionType = freerdp_settings_get_uint32(settings, FreeRDP_ConnectionType);
1173
1174
0
  file->ScreenModeId = freerdp_settings_get_bool(settings, FreeRDP_Fullscreen) ? 2 : 1;
1175
1176
0
  LoadBalanceInfoLength = freerdp_settings_get_uint32(settings, FreeRDP_LoadBalanceInfoLength);
1177
0
  if (LoadBalanceInfoLength > 0)
1178
0
  {
1179
0
    const BYTE* LoadBalanceInfo =
1180
0
        freerdp_settings_get_pointer(settings, FreeRDP_LoadBalanceInfo);
1181
0
    file->LoadBalanceInfo = calloc(LoadBalanceInfoLength + 1, 1);
1182
0
    if (!file->LoadBalanceInfo)
1183
0
      return FALSE;
1184
0
    memcpy(file->LoadBalanceInfo, LoadBalanceInfo, LoadBalanceInfoLength);
1185
0
  }
1186
1187
0
  if (freerdp_settings_get_bool(settings, FreeRDP_AudioPlayback))
1188
0
    file->AudioMode = AUDIO_MODE_REDIRECT;
1189
0
  else if (freerdp_settings_get_bool(settings, FreeRDP_RemoteConsoleAudio))
1190
0
    file->AudioMode = AUDIO_MODE_PLAY_ON_SERVER;
1191
0
  else
1192
0
    file->AudioMode = AUDIO_MODE_NONE;
1193
1194
  /* The gateway hostname should also contain a port specifier unless it is the default port 443
1195
   */
1196
0
  GatewayHostname = freerdp_settings_get_string(settings, FreeRDP_GatewayHostname);
1197
0
  if (GatewayHostname)
1198
0
  {
1199
0
    const UINT32 GatewayPort = freerdp_settings_get_uint32(settings, FreeRDP_GatewayPort);
1200
0
    freerdp_client_file_string_check_free(file->GatewayHostname);
1201
0
    if (GatewayPort == 443)
1202
0
      file->GatewayHostname = _strdup(GatewayHostname);
1203
0
    else
1204
0
    {
1205
0
      int length = _scprintf("%s:%" PRIu32, GatewayHostname, GatewayPort);
1206
0
      if (length < 0)
1207
0
        return FALSE;
1208
1209
0
      file->GatewayHostname = (char*)malloc((size_t)length + 1);
1210
0
      if (!file->GatewayHostname)
1211
0
        return FALSE;
1212
1213
0
      if (sprintf_s(file->GatewayHostname, (size_t)length + 1, "%s:%" PRIu32, GatewayHostname,
1214
0
                    GatewayPort) < 0)
1215
0
        return FALSE;
1216
0
    }
1217
0
    if (!file->GatewayHostname)
1218
0
      return FALSE;
1219
0
  }
1220
1221
0
  if (freerdp_settings_get_bool(settings, FreeRDP_GatewayArmTransport))
1222
0
    file->ResourceProvider = _strdup(str_resourceprovider_arm);
1223
1224
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdWvdEndpointPool, &file->WvdEndpointPool))
1225
0
    return FALSE;
1226
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdGeo, &file->geo))
1227
0
    return FALSE;
1228
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdArmpath, &file->armpath))
1229
0
    return FALSE;
1230
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdAadtenantid, &file->aadtenantid))
1231
0
    return FALSE;
1232
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdDiagnosticserviceurl,
1233
0
                         &file->diagnosticserviceurl))
1234
0
    return FALSE;
1235
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdHubdiscoverygeourl,
1236
0
                         &file->hubdiscoverygeourl))
1237
0
    return FALSE;
1238
0
  if (!rdp_opt_duplicate(settings, FreeRDP_GatewayAvdActivityhint, &file->activityhint))
1239
0
    return FALSE;
1240
1241
0
  file->AudioCaptureMode =
1242
0
      WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_AudioCapture));
1243
0
  file->BitmapCachePersistEnable = WINPR_ASSERTING_INT_CAST(
1244
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled));
1245
0
  file->Compression = WINPR_ASSERTING_INT_CAST(
1246
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_CompressionEnabled));
1247
0
  file->AuthenticationLevel = freerdp_settings_get_uint32(settings, FreeRDP_AuthenticationLevel);
1248
0
  file->GatewayUsageMethod = freerdp_settings_get_uint32(settings, FreeRDP_GatewayUsageMethod);
1249
0
  file->GatewayCredentialsSource =
1250
0
      freerdp_settings_get_uint32(settings, FreeRDP_GatewayCredentialsSource);
1251
0
  file->PromptCredentialOnce = WINPR_ASSERTING_INT_CAST(
1252
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_GatewayUseSameCredentials));
1253
0
  file->PromptForCredentials = WINPR_ASSERTING_INT_CAST(
1254
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_PromptForCredentials));
1255
0
  file->RemoteApplicationMode = WINPR_ASSERTING_INT_CAST(
1256
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_RemoteApplicationMode));
1257
0
  if (!FILE_POPULATE_STRING(&file->GatewayAccessToken, settings, FreeRDP_GatewayAccessToken) ||
1258
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationProgram, settings,
1259
0
                            FreeRDP_RemoteApplicationProgram) ||
1260
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationName, settings,
1261
0
                            FreeRDP_RemoteApplicationName) ||
1262
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationIcon, settings,
1263
0
                            FreeRDP_RemoteApplicationIcon) ||
1264
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationFile, settings,
1265
0
                            FreeRDP_RemoteApplicationFile) ||
1266
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationGuid, settings,
1267
0
                            FreeRDP_RemoteApplicationGuid) ||
1268
0
      !FILE_POPULATE_STRING(&file->RemoteApplicationCmdLine, settings,
1269
0
                            FreeRDP_RemoteApplicationCmdLine))
1270
0
    return FALSE;
1271
0
  file->SpanMonitors =
1272
0
      WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_SpanMonitors));
1273
0
  file->UseMultiMon =
1274
0
      WINPR_ASSERTING_INT_CAST(UINT32, freerdp_settings_get_bool(settings, FreeRDP_UseMultimon));
1275
0
  file->AllowDesktopComposition = WINPR_ASSERTING_INT_CAST(
1276
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_AllowDesktopComposition));
1277
0
  file->AllowFontSmoothing = WINPR_ASSERTING_INT_CAST(
1278
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_AllowFontSmoothing));
1279
0
  file->DisableWallpaper = WINPR_ASSERTING_INT_CAST(
1280
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableWallpaper));
1281
0
  file->DisableFullWindowDrag = WINPR_ASSERTING_INT_CAST(
1282
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableFullWindowDrag));
1283
0
  file->DisableMenuAnims = WINPR_ASSERTING_INT_CAST(
1284
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableMenuAnims));
1285
0
  file->DisableThemes = WINPR_ASSERTING_INT_CAST(
1286
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_DisableThemes));
1287
0
  file->BandwidthAutoDetect = (freerdp_settings_get_uint32(settings, FreeRDP_ConnectionType) >=
1288
0
                               CONNECTION_TYPE_AUTODETECT)
1289
0
                                  ? TRUE
1290
0
                                  : FALSE;
1291
0
  file->NetworkAutoDetect =
1292
0
      freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect) ? 1 : 0;
1293
0
  file->AutoReconnectionEnabled = WINPR_ASSERTING_INT_CAST(
1294
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_AutoReconnectionEnabled));
1295
0
  file->RedirectSmartCards = WINPR_ASSERTING_INT_CAST(
1296
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_RedirectSmartCards));
1297
0
  file->RedirectWebauthN = WINPR_ASSERTING_INT_CAST(
1298
0
      UINT32, freerdp_settings_get_bool(settings, FreeRDP_RedirectWebAuthN));
1299
1300
0
  redirectCameras =
1301
0
      freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "device:");
1302
0
  if (redirectCameras)
1303
0
  {
1304
0
    char* str =
1305
0
        freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "encode:");
1306
0
    file->EncodeRedirectedVideoCapture = 0;
1307
0
    if (str)
1308
0
    {
1309
0
      unsigned long val = 0;
1310
0
      errno = 0;
1311
0
      val = strtoul(str, NULL, 0);
1312
0
      if ((val < UINT32_MAX) && (errno == 0))
1313
0
        file->EncodeRedirectedVideoCapture = (UINT32)val;
1314
0
    }
1315
0
    free(str);
1316
1317
0
    str = freerdp_client_channel_args_to_string(settings, RDPECAM_DVC_CHANNEL_NAME, "quality:");
1318
0
    file->RedirectedVideoCaptureEncodingQuality = 0;
1319
0
    if (str)
1320
0
    {
1321
0
      unsigned long val = 0;
1322
0
      errno = 0;
1323
0
      val = strtoul(str, NULL, 0);
1324
0
      if ((val <= 2) && (errno == 0))
1325
0
      {
1326
0
        file->RedirectedVideoCaptureEncodingQuality = (UINT32)val;
1327
0
      }
1328
0
    }
1329
0
    free(str);
1330
1331
0
    file->RedirectCameras = redirectCameras;
1332
0
  }
1333
0
  else
1334
0
    freerdp_client_file_string_reset(&file->RedirectCameras);
1335
1336
#ifdef CHANNEL_URBDRC_CLIENT
1337
  char* redirectUsb =
1338
      freerdp_client_channel_args_to_string(settings, URBDRC_CHANNEL_NAME, "device:");
1339
  if (redirectUsb)
1340
    file->UsbDevicesToRedirect = redirectUsb;
1341
  else
1342
    freerdp_client_file_string_reset(&file->UsbDevicesToRedirect);
1343
#endif
1344
1345
0
  file->RedirectClipboard =
1346
0
      freerdp_settings_get_bool(settings, FreeRDP_RedirectClipboard) ? 1 : 0;
1347
0
  file->RedirectPrinters = freerdp_settings_get_bool(settings, FreeRDP_RedirectPrinters) ? 1 : 0;
1348
0
  file->RedirectDrives = freerdp_settings_get_bool(settings, FreeRDP_RedirectDrives) ? 1 : 0;
1349
0
  file->RdgIsKdcProxy = freerdp_settings_get_bool(settings, FreeRDP_KerberosRdgIsProxy) ? 1 : 0;
1350
0
  file->RedirectComPorts = (freerdp_settings_get_bool(settings, FreeRDP_RedirectSerialPorts) ||
1351
0
                            freerdp_settings_get_bool(settings, FreeRDP_RedirectParallelPorts));
1352
0
  file->RedirectLocation =
1353
0
      freerdp_dynamic_channel_collection_find(settings, LOCATION_CHANNEL_NAME) ? TRUE : FALSE;
1354
0
  if (!FILE_POPULATE_STRING(&file->DrivesToRedirect, settings, FreeRDP_DrivesToRedirect) ||
1355
0
      !FILE_POPULATE_STRING(&file->PreconnectionBlob, settings, FreeRDP_PreconnectionBlob) ||
1356
0
      !FILE_POPULATE_STRING(&file->KdcProxyName, settings, FreeRDP_KerberosKdcUrl))
1357
0
    return FALSE;
1358
1359
0
  {
1360
0
    size_t offset = 0;
1361
0
    UINT32 count = freerdp_settings_get_uint32(settings, FreeRDP_NumMonitorIds);
1362
0
    const UINT32* MonitorIds = freerdp_settings_get_pointer(settings, FreeRDP_MonitorIds);
1363
    /* String size: 10 char UINT32 max string length, 1 char separator, one element NULL */
1364
0
    size_t size = count * (10 + 1) + 1;
1365
1366
0
    char* str = calloc(size, sizeof(char));
1367
0
    for (UINT32 x = 0; x < count; x++)
1368
0
    {
1369
0
      int rc = _snprintf(&str[offset], size - offset, "%" PRIu32 ",", MonitorIds[x]);
1370
0
      if (rc <= 0)
1371
0
      {
1372
0
        free(str);
1373
0
        return FALSE;
1374
0
      }
1375
0
      offset += (size_t)rc;
1376
0
    }
1377
0
    if (offset > 0)
1378
0
      str[offset - 1] = '\0';
1379
0
    freerdp_client_file_string_check_free(file->SelectedMonitors);
1380
0
    file->SelectedMonitors = str;
1381
0
  }
1382
1383
0
  file->KeyboardHook = freerdp_settings_get_uint32(settings, FreeRDP_KeyboardHook);
1384
1385
0
  return TRUE;
1386
0
}
1387
1388
BOOL freerdp_client_write_rdp_file(const rdpFile* file, const char* name, BOOL unicode)
1389
0
{
1390
0
  int status = 0;
1391
0
  WCHAR* unicodestr = NULL;
1392
1393
0
  if (!file || !name)
1394
0
    return FALSE;
1395
1396
0
  const size_t size = freerdp_client_write_rdp_file_buffer(file, NULL, 0);
1397
0
  if (size == 0)
1398
0
    return FALSE;
1399
0
  char* buffer = calloc(size + 1ULL, sizeof(char));
1400
1401
0
  if (freerdp_client_write_rdp_file_buffer(file, buffer, size + 1) != size)
1402
0
  {
1403
0
    WLog_ERR(TAG, "freerdp_client_write_rdp_file: error writing to output buffer");
1404
0
    free(buffer);
1405
0
    return FALSE;
1406
0
  }
1407
1408
0
  FILE* fp = winpr_fopen(name, "w+b");
1409
1410
0
  if (fp)
1411
0
  {
1412
0
    if (unicode)
1413
0
    {
1414
0
      size_t len = 0;
1415
0
      unicodestr = ConvertUtf8NToWCharAlloc(buffer, size, &len);
1416
1417
0
      if (!unicodestr)
1418
0
      {
1419
0
        free(buffer);
1420
0
        (void)fclose(fp);
1421
0
        return FALSE;
1422
0
      }
1423
1424
      /* Write multi-byte header */
1425
0
      if ((fwrite(BOM_UTF16_LE, sizeof(BYTE), 2, fp) != 2) ||
1426
0
          (fwrite(unicodestr, sizeof(WCHAR), len, fp) != len))
1427
0
      {
1428
0
        free(buffer);
1429
0
        free(unicodestr);
1430
0
        (void)fclose(fp);
1431
0
        return FALSE;
1432
0
      }
1433
1434
0
      free(unicodestr);
1435
0
    }
1436
0
    else
1437
0
    {
1438
0
      if (fwrite(buffer, 1, size, fp) != size)
1439
0
      {
1440
0
        free(buffer);
1441
0
        (void)fclose(fp);
1442
0
        return FALSE;
1443
0
      }
1444
0
    }
1445
1446
0
    (void)fflush(fp);
1447
0
    status = fclose(fp);
1448
0
  }
1449
1450
0
  free(buffer);
1451
0
  return (status == 0) ? TRUE : FALSE;
1452
0
}
1453
1454
WINPR_ATTR_FORMAT_ARG(3, 4)
1455
static SSIZE_T freerdp_client_write_setting_to_buffer(char** buffer, size_t* bufferSize,
1456
                                                      WINPR_FORMAT_ARG const char* fmt, ...)
1457
0
{
1458
0
  va_list ap = WINPR_C_ARRAY_INIT;
1459
0
  SSIZE_T len = 0;
1460
0
  char* buf = NULL;
1461
0
  size_t bufSize = 0;
1462
1463
0
  if (!buffer || !bufferSize || !fmt)
1464
0
    return -1;
1465
1466
0
  buf = *buffer;
1467
0
  bufSize = *bufferSize;
1468
1469
0
  va_start(ap, fmt);
1470
0
  len = vsnprintf(buf, bufSize, fmt, ap);
1471
0
  va_end(ap);
1472
0
  if (len < 0)
1473
0
    return -1;
1474
1475
  /* _snprintf doesn't add the ending \0 to its return value */
1476
0
  ++len;
1477
1478
  /* we just want to know the size - return it */
1479
0
  if (!buf && !bufSize)
1480
0
    return len;
1481
1482
0
  if (!buf)
1483
0
    return -1;
1484
1485
  /* update buffer size and buffer position and replace \0 with \n */
1486
0
  if (bufSize >= (size_t)len)
1487
0
  {
1488
0
    *bufferSize -= (size_t)len;
1489
0
    buf[len - 1] = '\n';
1490
0
    *buffer = buf + len;
1491
0
  }
1492
0
  else
1493
0
    return -1;
1494
1495
0
  return len;
1496
0
}
1497
1498
static SSIZE_T write_int_parameters(const rdpFile* file, char* buffer, size_t size)
1499
0
{
1500
0
  WINPR_ASSERT(file);
1501
1502
0
  struct intentry_t
1503
0
  {
1504
0
    const char* key;
1505
0
    DWORD val;
1506
0
  };
1507
0
  const struct intentry_t settings[] = {
1508
0
    { key_int_use_multimon, file->UseMultiMon },
1509
0
    { key_int_maximizetocurrentdisplays, file->MaximizeToCurrentDisplays },
1510
0
    { key_int_singlemoninwindowedmode, file->SingleMonInWindowedMode },
1511
0
    { key_int_screen_mode_id, file->ScreenModeId },
1512
0
    { key_int_span_monitors, file->SpanMonitors },
1513
0
    { key_int_smart_sizing, file->SmartSizing },
1514
0
    { key_int_dynamic_resolution, file->DynamicResolution },
1515
0
    { key_int_enablesuperpan, file->EnableSuperSpan },
1516
0
    { key_int_superpanaccelerationfactor, file->SuperSpanAccelerationFactor },
1517
0
    { key_int_desktopwidth, file->DesktopWidth },
1518
0
    { key_int_desktopheight, file->DesktopHeight },
1519
0
    { key_int_desktop_size_id, file->DesktopSizeId },
1520
0
    { key_int_session_bpp, file->SessionBpp },
1521
0
    { key_int_desktopscalefactor, file->DesktopScaleFactor },
1522
0
    { key_int_compression, file->Compression },
1523
0
    { key_int_keyboardhook, file->KeyboardHook },
1524
0
    { key_int_disable_ctrl_alt_del, file->DisableCtrlAltDel },
1525
0
    { key_int_audiomode, file->AudioMode },
1526
0
    { key_int_audioqualitymode, file->AudioQualityMode },
1527
0
    { key_int_audiocapturemode, file->AudioCaptureMode },
1528
0
    { key_int_encode_redirected_video_capture, file->EncodeRedirectedVideoCapture },
1529
0
    { key_int_redirected_video_capture_encoding_quality,
1530
0
      file->RedirectedVideoCaptureEncodingQuality },
1531
0
    { key_int_videoplaybackmode, file->VideoPlaybackMode },
1532
0
    { key_int_connection_type, file->ConnectionType },
1533
0
    { key_int_networkautodetect, file->NetworkAutoDetect },
1534
0
    { key_int_bandwidthautodetect, file->BandwidthAutoDetect },
1535
0
    { key_int_pinconnectionbar, file->PinConnectionBar },
1536
0
    { key_int_displayconnectionbar, file->DisplayConnectionBar },
1537
0
    { key_int_workspaceid, file->WorkspaceId },
1538
0
    { key_int_enableworkspacereconnect, file->EnableWorkspaceReconnect },
1539
0
    { key_int_disable_wallpaper, file->DisableWallpaper },
1540
0
    { key_int_allow_font_smoothing, file->AllowFontSmoothing },
1541
0
    { key_int_allow_desktop_composition, file->AllowDesktopComposition },
1542
0
    { key_int_disable_full_window_drag, file->DisableFullWindowDrag },
1543
0
    { key_int_disable_menu_anims, file->DisableMenuAnims },
1544
0
    { key_int_disable_themes, file->DisableThemes },
1545
0
    { key_int_disable_cursor_setting, file->DisableCursorSetting },
1546
0
    { key_int_bitmapcachesize, file->BitmapCacheSize },
1547
0
    { key_int_bitmapcachepersistenable, file->BitmapCachePersistEnable },
1548
0
    { key_int_server_port, file->ServerPort },
1549
0
    { key_int_redirectdrives, file->RedirectDrives },
1550
0
    { key_int_redirectprinters, file->RedirectPrinters },
1551
0
    { key_int_redirectcomports, file->RedirectComPorts },
1552
0
    { key_int_redirectlocation, file->RedirectLocation },
1553
0
    { key_int_redirectsmartcards, file->RedirectSmartCards },
1554
0
    { key_int_redirectclipboard, file->RedirectClipboard },
1555
0
    { key_int_redirectposdevices, file->RedirectPosDevices },
1556
0
    { key_int_redirectdirectx, file->RedirectDirectX },
1557
0
    { key_int_disableprinterredirection, file->DisablePrinterRedirection },
1558
0
    { key_int_disableclipboardredirection, file->DisableClipboardRedirection },
1559
0
    { key_int_connect_to_console, file->ConnectToConsole },
1560
0
    { key_int_administrative_session, file->AdministrativeSession },
1561
0
    { key_int_autoreconnection_enabled, file->AutoReconnectionEnabled },
1562
0
    { key_int_autoreconnect_max_retries, file->AutoReconnectMaxRetries },
1563
0
    { key_int_public_mode, file->PublicMode },
1564
0
    { key_int_authentication_level, file->AuthenticationLevel },
1565
0
    { key_int_promptcredentialonce, file->PromptCredentialOnce },
1566
0
    { key_int_prompt_for_credentials, file->PromptForCredentials },
1567
0
    { key_int_negotiate_security_layer, file->NegotiateSecurityLayer },
1568
0
    { key_int_enablecredsspsupport, file->EnableCredSSPSupport },
1569
0
    { key_int_enablerdsaadauth, file->EnableRdsAadAuth },
1570
0
    { key_int_remoteapplicationmode, file->RemoteApplicationMode },
1571
0
    { key_int_remoteapplicationexpandcmdline, file->RemoteApplicationExpandCmdLine },
1572
0
    { key_int_remoteapplicationexpandworkingdir, file->RemoteApplicationExpandWorkingDir },
1573
0
    { key_int_disableconnectionsharing, file->DisableConnectionSharing },
1574
0
    { key_int_disableremoteappcapscheck, file->DisableRemoteAppCapsCheck },
1575
0
    { key_int_gatewayusagemethod, file->GatewayUsageMethod },
1576
0
    { key_int_gatewayprofileusagemethod, file->GatewayProfileUsageMethod },
1577
0
    { key_int_gatewaycredentialssource, file->GatewayCredentialsSource },
1578
0
    { key_int_use_redirection_server_name, file->UseRedirectionServerName },
1579
0
    { key_int_rdgiskdcproxy, file->RdgIsKdcProxy },
1580
0
    { key_int_redirectwebauthn, file->RedirectWebauthN }
1581
0
  };
1582
1583
0
  SSIZE_T totalSize = 0;
1584
0
  for (size_t x = 0; x < ARRAYSIZE(settings); x++)
1585
0
  {
1586
0
    const struct intentry_t* cur = &settings[x];
1587
0
    if (~cur->val)
1588
0
    {
1589
0
      const SSIZE_T res = freerdp_client_write_setting_to_buffer(
1590
0
          &buffer, &size, "%s:i:%" PRIu32, cur->key, cur->val);
1591
0
      if (res < 0)
1592
0
        return res;
1593
0
      totalSize += res;
1594
0
    }
1595
0
  }
1596
1597
0
  return totalSize;
1598
0
}
1599
1600
static SSIZE_T write_string_parameters(const rdpFile* file, char* buffer, size_t size)
1601
0
{
1602
0
  WINPR_ASSERT(file);
1603
1604
0
  struct strentry_t
1605
0
  {
1606
0
    const char* key;
1607
0
    const char* val;
1608
0
  };
1609
0
  const struct strentry_t settings[] = {
1610
0
    { key_str_username, file->Username },
1611
0
    { key_str_domain, file->Domain },
1612
0
    { key_str_password, file->Password },
1613
0
    { key_str_full_address, file->FullAddress },
1614
0
    { key_str_alternate_full_address, file->AlternateFullAddress },
1615
0
    { key_str_usbdevicestoredirect, file->UsbDevicesToRedirect },
1616
0
    { key_str_camerastoredirect, file->RedirectCameras },
1617
0
    { key_str_loadbalanceinfo, file->LoadBalanceInfo },
1618
0
    { key_str_remoteapplicationname, file->RemoteApplicationName },
1619
0
    { key_str_remoteapplicationicon, file->RemoteApplicationIcon },
1620
0
    { key_str_remoteapplicationprogram, file->RemoteApplicationProgram },
1621
0
    { key_str_remoteapplicationfile, file->RemoteApplicationFile },
1622
0
    { key_str_remoteapplicationguid, file->RemoteApplicationGuid },
1623
0
    { key_str_remoteapplicationcmdline, file->RemoteApplicationCmdLine },
1624
0
    { key_str_alternate_shell, file->AlternateShell },
1625
0
    { key_str_shell_working_directory, file->ShellWorkingDirectory },
1626
0
    { key_str_gatewayhostname, file->GatewayHostname },
1627
0
    { key_str_resourceprovider, file->ResourceProvider },
1628
0
    { key_str_wvd, file->WvdEndpointPool },
1629
0
    { key_str_geo, file->geo },
1630
0
    { key_str_armpath, file->armpath },
1631
0
    { key_str_aadtenantid, file->aadtenantid },
1632
0
    { key_str_diagnosticserviceurl, file->diagnosticserviceurl },
1633
0
    { key_str_hubdiscoverygeourl, file->hubdiscoverygeourl },
1634
0
    { key_str_activityhint, file->activityhint },
1635
0
    { key_str_gatewayaccesstoken, file->GatewayAccessToken },
1636
0
    { key_str_kdcproxyname, file->KdcProxyName },
1637
0
    { key_str_drivestoredirect, file->DrivesToRedirect },
1638
0
    { key_str_devicestoredirect, file->DevicesToRedirect },
1639
0
    { key_str_winposstr, file->WinPosStr },
1640
0
    { key_str_pcb, file->PreconnectionBlob },
1641
0
    { key_str_selectedmonitors, file->SelectedMonitors }
1642
0
  };
1643
1644
0
  SSIZE_T totalSize = 0;
1645
0
  for (size_t x = 0; x < ARRAYSIZE(settings); x++)
1646
0
  {
1647
0
    const struct strentry_t* cur = &settings[x];
1648
0
    if (~(size_t)(cur->val))
1649
0
    {
1650
0
      const SSIZE_T res = freerdp_client_write_setting_to_buffer(&buffer, &size, "%s:s:%s",
1651
0
                                                                 cur->key, cur->val);
1652
0
      if (res < 0)
1653
0
        return res;
1654
0
      totalSize += res;
1655
0
    }
1656
0
  }
1657
1658
0
  return totalSize;
1659
0
}
1660
1661
static SSIZE_T write_custom_parameters(const rdpFile* file, char* buffer, size_t size)
1662
0
{
1663
0
  WINPR_ASSERT(file);
1664
1665
0
  SSIZE_T totalSize = 0;
1666
  /* custom parameters */
1667
0
  for (size_t i = 0; i < file->lineCount; ++i)
1668
0
  {
1669
0
    SSIZE_T res = -1;
1670
0
    const rdpFileLine* curLine = &file->lines[i];
1671
1672
0
    if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_INTEGER)
1673
0
      res = freerdp_client_write_setting_to_buffer(&buffer, &size, "%s:i:%" PRIu32,
1674
0
                                                   curLine->name, (UINT32)curLine->iValue);
1675
0
    else if (curLine->flags & RDP_FILE_LINE_FLAG_TYPE_STRING)
1676
0
      res = freerdp_client_write_setting_to_buffer(&buffer, &size, "%s:s:%s", curLine->name,
1677
0
                                                   curLine->sValue);
1678
0
    if (res < 0)
1679
0
      return res;
1680
1681
0
    totalSize += res;
1682
0
  }
1683
0
  return totalSize;
1684
0
}
1685
1686
size_t freerdp_client_write_rdp_file_buffer(const rdpFile* file, char* buffer, size_t size)
1687
0
{
1688
0
  size_t totalSize = 0;
1689
1690
0
  if (!file)
1691
0
    return 0;
1692
1693
  /* either buffer and size are null or non-null */
1694
0
  if ((!buffer || !size) && (buffer || size))
1695
0
    return 0;
1696
1697
  /* integer parameters */
1698
0
  const SSIZE_T intsize = write_int_parameters(file, buffer, size);
1699
0
  if (intsize < 0)
1700
0
    return 0;
1701
0
  totalSize += (size_t)intsize;
1702
0
  if (buffer)
1703
0
    buffer += intsize;
1704
1705
  /* string parameters */
1706
0
  const SSIZE_T stringsize = write_string_parameters(file, buffer, size);
1707
0
  if (stringsize < 0)
1708
0
    return 0;
1709
0
  totalSize += (size_t)stringsize;
1710
0
  if (buffer)
1711
0
    buffer += stringsize;
1712
1713
  /* custom parameters */
1714
0
  const SSIZE_T customsize = write_custom_parameters(file, buffer, size);
1715
0
  if (customsize < 0)
1716
0
    return 0;
1717
0
  totalSize += (size_t)customsize;
1718
0
  return totalSize;
1719
0
}
1720
1721
static ADDIN_ARGV* rdp_file_to_args(const char* channel, const char* values)
1722
0
{
1723
0
  size_t count = 0;
1724
0
  char** p = NULL;
1725
0
  ADDIN_ARGV* args = freerdp_addin_argv_new(0, NULL);
1726
0
  if (!args)
1727
0
    return NULL;
1728
0
  if (!freerdp_addin_argv_add_argument(args, channel))
1729
0
    goto fail;
1730
0
1731
0
  p = CommandLineParseCommaSeparatedValues(values, &count);
1732
0
  for (size_t x = 0; x < count; x++)
1733
0
  {
1734
0
    BOOL rc = 0;
1735
0
    const char* val = p[x];
1736
0
    const size_t len = strlen(val) + 8;
1737
0
    char* str = calloc(len, sizeof(char));
1738
0
    if (!str)
1739
0
      goto fail;
1740
0
1741
0
    (void)_snprintf(str, len, "device:%s", val);
1742
0
    rc = freerdp_addin_argv_add_argument(args, str);
1743
0
    free(str);
1744
0
    if (!rc)
1745
0
      goto fail;
1746
0
  }
1747
0
  CommandLineParserFree(p);
1748
0
  return args;
1749
0
1750
0
fail:
1751
0
  CommandLineParserFree(p);
1752
0
  freerdp_addin_argv_free(args);
1753
0
  return NULL;
1754
0
}
1755
1756
BOOL freerdp_client_populate_settings_from_rdp_file_unchecked(const rdpFile* file,
1757
                                                              rdpSettings* settings)
1758
0
{
1759
0
  if (!file || !settings)
1760
0
    return FALSE;
1761
1762
  /* Start with connection type.
1763
   * This setting initializes certain defaults which might be overridden by later options.
1764
   */
1765
0
  if (~file->BandwidthAutoDetect)
1766
0
  {
1767
0
    if (file->BandwidthAutoDetect != 0)
1768
0
    {
1769
0
      if ((~file->NetworkAutoDetect) && (file->NetworkAutoDetect == 0))
1770
0
      {
1771
0
        WLog_WARN(TAG,
1772
0
                  "Got networkautodetect:i:%" PRIu32 " and bandwidthautodetect:i:%" PRIu32
1773
0
                  ". Correcting to networkautodetect:i:1",
1774
0
                  file->NetworkAutoDetect, file->BandwidthAutoDetect);
1775
0
        WLog_WARN(TAG,
1776
0
                  "Add networkautodetect:i:1 to your RDP file to eliminate this warning.");
1777
0
      }
1778
0
    }
1779
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_NetworkAutoDetect,
1780
0
                                   (file->BandwidthAutoDetect != 0) ||
1781
0
                                       (file->NetworkAutoDetect != 0)))
1782
0
      return FALSE;
1783
0
  }
1784
1785
0
  if (~file->NetworkAutoDetect)
1786
0
  {
1787
0
    if (file->NetworkAutoDetect != 0)
1788
0
    {
1789
0
      if ((~file->BandwidthAutoDetect) && (file->BandwidthAutoDetect == 0))
1790
0
      {
1791
0
        WLog_WARN(TAG,
1792
0
                  "Got networkautodetect:i:%" PRIu32 " and bandwidthautodetect:i:%" PRIu32
1793
0
                  ". Correcting to bandwidthautodetect:i:1",
1794
0
                  file->NetworkAutoDetect, file->BandwidthAutoDetect);
1795
0
        WLog_WARN(
1796
0
            TAG, "Add bandwidthautodetect:i:1 to your RDP file to eliminate this warning.");
1797
0
      }
1798
0
    }
1799
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_NetworkAutoDetect,
1800
0
                                   (file->BandwidthAutoDetect != 0) ||
1801
0
                                       (file->NetworkAutoDetect != 0)))
1802
0
      return FALSE;
1803
0
  }
1804
1805
0
  if (~((size_t)file->Domain))
1806
0
  {
1807
0
    if (!freerdp_settings_set_string(settings, FreeRDP_Domain, file->Domain))
1808
0
      return FALSE;
1809
0
  }
1810
1811
0
  if (~((size_t)file->Username))
1812
0
  {
1813
0
    char* user = NULL;
1814
0
    char* domain = NULL;
1815
1816
0
    if (!freerdp_parse_username(file->Username, &user, &domain))
1817
0
      return FALSE;
1818
1819
0
    if (!freerdp_settings_set_string(settings, FreeRDP_Username, user))
1820
0
      return FALSE;
1821
1822
0
    if (!(~((size_t)file->Domain)) && domain)
1823
0
    {
1824
0
      if (!freerdp_settings_set_string(settings, FreeRDP_Domain, domain))
1825
0
        return FALSE;
1826
0
    }
1827
1828
0
    free(user);
1829
0
    free(domain);
1830
0
  }
1831
1832
0
  if (~((size_t)file->Password))
1833
0
  {
1834
0
    if (!freerdp_settings_set_string(settings, FreeRDP_Password, file->Password))
1835
0
      return FALSE;
1836
0
  }
1837
1838
0
  {
1839
0
    const char* address = NULL;
1840
1841
    /* With MSTSC alternate full address always wins,
1842
     * so mimic this. */
1843
0
    if (~((size_t)file->AlternateFullAddress))
1844
0
      address = file->AlternateFullAddress;
1845
0
    else if (~((size_t)file->FullAddress))
1846
0
      address = file->FullAddress;
1847
1848
0
    if (address)
1849
0
    {
1850
0
      int port = -1;
1851
0
      char* host = NULL;
1852
1853
0
      if (!freerdp_parse_hostname(address, &host, &port))
1854
0
        return FALSE;
1855
1856
0
      const BOOL rc = freerdp_settings_set_string(settings, FreeRDP_ServerHostname, host);
1857
0
      free(host);
1858
0
      if (!rc)
1859
0
        return FALSE;
1860
1861
0
      if (port > 0)
1862
0
      {
1863
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, (UINT32)port))
1864
0
          return FALSE;
1865
0
      }
1866
0
    }
1867
0
  }
1868
1869
0
  if (~file->ServerPort)
1870
0
  {
1871
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_ServerPort, file->ServerPort))
1872
0
      return FALSE;
1873
0
  }
1874
1875
0
  if (~file->DesktopSizeId)
1876
0
  {
1877
0
    switch (file->DesktopSizeId)
1878
0
    {
1879
0
      case 0:
1880
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 640))
1881
0
          return FALSE;
1882
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 480))
1883
0
          return FALSE;
1884
0
        break;
1885
0
      case 1:
1886
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 800))
1887
0
          return FALSE;
1888
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 600))
1889
0
          return FALSE;
1890
0
        break;
1891
0
      case 2:
1892
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1024))
1893
0
          return FALSE;
1894
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 768))
1895
0
          return FALSE;
1896
0
        break;
1897
0
      case 3:
1898
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1280))
1899
0
          return FALSE;
1900
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 1024))
1901
0
          return FALSE;
1902
0
        break;
1903
0
      case 4:
1904
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, 1600))
1905
0
          return FALSE;
1906
0
        if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, 1200))
1907
0
          return FALSE;
1908
0
        break;
1909
0
      default:
1910
0
        WLog_WARN(TAG, "Unsupported 'desktop size id' value %" PRIu32, file->DesktopSizeId);
1911
0
        break;
1912
0
    }
1913
0
  }
1914
1915
0
  if (~file->DesktopWidth)
1916
0
  {
1917
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, file->DesktopWidth))
1918
0
      return FALSE;
1919
0
  }
1920
1921
0
  if (~file->DesktopHeight)
1922
0
  {
1923
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, file->DesktopHeight))
1924
0
      return FALSE;
1925
0
  }
1926
1927
0
  if (~file->SessionBpp)
1928
0
  {
1929
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_ColorDepth, file->SessionBpp))
1930
0
      return FALSE;
1931
0
  }
1932
1933
0
  if (~file->ConnectToConsole)
1934
0
  {
1935
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_ConsoleSession,
1936
0
                                   file->ConnectToConsole != 0))
1937
0
      return FALSE;
1938
0
  }
1939
1940
0
  if (~file->AdministrativeSession)
1941
0
  {
1942
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_ConsoleSession,
1943
0
                                   file->AdministrativeSession != 0))
1944
0
      return FALSE;
1945
0
  }
1946
1947
0
  if (~file->NegotiateSecurityLayer)
1948
0
  {
1949
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_NegotiateSecurityLayer,
1950
0
                                   file->NegotiateSecurityLayer != 0))
1951
0
      return FALSE;
1952
0
  }
1953
1954
0
  if (~file->EnableCredSSPSupport)
1955
0
  {
1956
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_NlaSecurity,
1957
0
                                   file->EnableCredSSPSupport != 0))
1958
0
      return FALSE;
1959
0
  }
1960
1961
0
  if (~file->EnableRdsAadAuth)
1962
0
  {
1963
0
    const BOOL val = file->EnableRdsAadAuth != 0;
1964
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_AadSecurity, val))
1965
0
      return FALSE;
1966
0
  }
1967
1968
0
  if (~((size_t)file->AlternateShell))
1969
0
  {
1970
0
    if (!freerdp_settings_set_string(settings, FreeRDP_AlternateShell, file->AlternateShell))
1971
0
      return FALSE;
1972
0
  }
1973
1974
0
  if (~((size_t)file->ShellWorkingDirectory))
1975
0
  {
1976
    /* ShellWorkingDir is used for either, shell working dir or remote app working dir */
1977
0
    FreeRDP_Settings_Keys_String targetId =
1978
0
        (~file->RemoteApplicationMode && file->RemoteApplicationMode != 0)
1979
0
            ? FreeRDP_RemoteApplicationWorkingDir
1980
0
            : FreeRDP_ShellWorkingDirectory;
1981
1982
0
    if (!freerdp_settings_set_string(settings, targetId, file->ShellWorkingDirectory))
1983
0
      return FALSE;
1984
0
  }
1985
1986
0
  if (~file->ScreenModeId)
1987
0
  {
1988
    /**
1989
     * Screen Mode Id:
1990
     * http://technet.microsoft.com/en-us/library/ff393692/
1991
     *
1992
     * This setting corresponds to the selection in the Display
1993
     * configuration slider on the Display tab under Options in RDC.
1994
     *
1995
     * Values:
1996
     *
1997
     * 1: The remote session will appear in a window.
1998
     * 2: The remote session will appear full screen.
1999
     */
2000
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_Fullscreen,
2001
0
                                   (file->ScreenModeId == 2) ? TRUE : FALSE))
2002
0
      return FALSE;
2003
0
  }
2004
2005
0
  if (~(file->SmartSizing))
2006
0
  {
2007
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_SmartSizing,
2008
0
                                   (file->SmartSizing == 1) ? TRUE : FALSE))
2009
0
      return FALSE;
2010
    /**
2011
     *  SmartSizingWidth and SmartSizingHeight:
2012
     *
2013
     *  Adding this option to use the DesktopHeight and DesktopWidth as
2014
     *  parameters for the SmartSizingWidth and SmartSizingHeight, as there
2015
     *  are no options for that in standard RDP files.
2016
     *
2017
     *  Equivalent of doing /smart-sizing:WxH
2018
     */
2019
0
    if (((~(file->DesktopWidth) && ~(file->DesktopHeight)) || ~(file->DesktopSizeId)) &&
2020
0
        (file->SmartSizing == 1))
2021
0
    {
2022
0
      if (!freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingWidth,
2023
0
                                       file->DesktopWidth))
2024
0
        return FALSE;
2025
0
      if (!freerdp_settings_set_uint32(settings, FreeRDP_SmartSizingHeight,
2026
0
                                       file->DesktopHeight))
2027
0
        return FALSE;
2028
0
    }
2029
0
  }
2030
2031
0
  if (~((size_t)file->LoadBalanceInfo))
2032
0
  {
2033
0
    const size_t len = strlen(file->LoadBalanceInfo);
2034
0
    if (!freerdp_settings_set_pointer_len(settings, FreeRDP_LoadBalanceInfo,
2035
0
                                          file->LoadBalanceInfo, len))
2036
0
      return FALSE;
2037
0
  }
2038
2039
0
  if (~file->AuthenticationLevel)
2040
0
  {
2041
    /**
2042
     * Authentication Level:
2043
     * http://technet.microsoft.com/en-us/library/ff393709/
2044
     *
2045
     * This setting corresponds to the selection in the If server authentication
2046
     * fails drop-down list on the Advanced tab under Options in RDC.
2047
     *
2048
     * Values:
2049
     *
2050
     * 0: If server authentication fails, connect to the computer without warning (Connect and
2051
     * don’t warn me). 1: If server authentication fails, do not establish a connection (Do not
2052
     * connect). 2: If server authentication fails, show a warning and allow me to connect or
2053
     * refuse the connection (Warn me). 3: No authentication requirement is specified.
2054
     */
2055
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_AuthenticationLevel,
2056
0
                                     file->AuthenticationLevel))
2057
0
      return FALSE;
2058
0
  }
2059
2060
0
  if (~file->ConnectionType)
2061
0
  {
2062
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_ConnectionType, file->ConnectionType))
2063
0
      return FALSE;
2064
0
  }
2065
2066
0
  if (~file->AudioMode)
2067
0
  {
2068
0
    switch (file->AudioMode)
2069
0
    {
2070
0
      case AUDIO_MODE_REDIRECT:
2071
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, FALSE))
2072
0
          return FALSE;
2073
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, TRUE))
2074
0
          return FALSE;
2075
0
        break;
2076
0
      case AUDIO_MODE_PLAY_ON_SERVER:
2077
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, TRUE))
2078
0
          return FALSE;
2079
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, FALSE))
2080
0
          return FALSE;
2081
0
        break;
2082
0
      case AUDIO_MODE_NONE:
2083
0
      default:
2084
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_AudioPlayback, FALSE))
2085
0
          return FALSE;
2086
0
        if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteConsoleAudio, FALSE))
2087
0
          return FALSE;
2088
0
        break;
2089
0
    }
2090
0
  }
2091
2092
0
  if (~file->AudioCaptureMode)
2093
0
  {
2094
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_AudioCapture, file->AudioCaptureMode != 0))
2095
0
      return FALSE;
2096
0
  }
2097
2098
0
  if (~file->Compression)
2099
0
  {
2100
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_CompressionEnabled,
2101
0
                                   file->Compression != 0))
2102
0
      return FALSE;
2103
0
  }
2104
2105
0
  if (~((size_t)file->GatewayHostname))
2106
0
  {
2107
0
    int port = -1;
2108
0
    char* host = NULL;
2109
2110
0
    if (!freerdp_parse_hostname(file->GatewayHostname, &host, &port))
2111
0
      return FALSE;
2112
2113
0
    const BOOL rc = freerdp_settings_set_string(settings, FreeRDP_GatewayHostname, host);
2114
0
    free(host);
2115
0
    if (!rc)
2116
0
      return FALSE;
2117
2118
0
    if (port > 0)
2119
0
    {
2120
0
      if (!freerdp_settings_set_uint32(settings, FreeRDP_GatewayPort, (UINT32)port))
2121
0
        return FALSE;
2122
0
    }
2123
0
  }
2124
2125
0
  if (~((size_t)file->ResourceProvider))
2126
0
  {
2127
0
    if (_stricmp(file->ResourceProvider, str_resourceprovider_arm) == 0)
2128
0
    {
2129
0
      if (!freerdp_settings_set_bool(settings, FreeRDP_GatewayArmTransport, TRUE))
2130
0
        return FALSE;
2131
0
    }
2132
0
  }
2133
2134
0
  if (~((size_t)file->WvdEndpointPool))
2135
0
  {
2136
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdWvdEndpointPool,
2137
0
                                     file->WvdEndpointPool))
2138
0
      return FALSE;
2139
0
  }
2140
2141
0
  if (~((size_t)file->geo))
2142
0
  {
2143
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdGeo, file->geo))
2144
0
      return FALSE;
2145
0
  }
2146
2147
0
  if (~((size_t)file->armpath))
2148
0
  {
2149
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdArmpath, file->armpath))
2150
0
      return FALSE;
2151
0
  }
2152
2153
0
  if (~((size_t)file->aadtenantid))
2154
0
  {
2155
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdAadtenantid,
2156
0
                                     file->aadtenantid))
2157
0
      return FALSE;
2158
0
  }
2159
2160
0
  if (~((size_t)file->diagnosticserviceurl))
2161
0
  {
2162
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdDiagnosticserviceurl,
2163
0
                                     file->diagnosticserviceurl))
2164
0
      return FALSE;
2165
0
  }
2166
2167
0
  if (~((size_t)file->hubdiscoverygeourl))
2168
0
  {
2169
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdHubdiscoverygeourl,
2170
0
                                     file->hubdiscoverygeourl))
2171
0
      return FALSE;
2172
0
  }
2173
2174
0
  if (~((size_t)file->activityhint))
2175
0
  {
2176
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAvdActivityhint,
2177
0
                                     file->activityhint))
2178
0
      return FALSE;
2179
0
  }
2180
2181
0
  if (~((size_t)file->GatewayAccessToken))
2182
0
  {
2183
0
    if (!freerdp_settings_set_string(settings, FreeRDP_GatewayAccessToken,
2184
0
                                     file->GatewayAccessToken))
2185
0
      return FALSE;
2186
0
  }
2187
2188
0
  if (~file->GatewayUsageMethod)
2189
0
  {
2190
0
    if (!freerdp_set_gateway_usage_method(settings, file->GatewayUsageMethod))
2191
0
      return FALSE;
2192
0
  }
2193
2194
0
  if (~file->PromptCredentialOnce)
2195
0
  {
2196
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_GatewayUseSameCredentials,
2197
0
                                   file->PromptCredentialOnce != 0))
2198
0
      return FALSE;
2199
0
  }
2200
2201
0
  if (~file->PromptForCredentials)
2202
0
  {
2203
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_PromptForCredentials,
2204
0
                                   file->PromptForCredentials != 0))
2205
0
      return FALSE;
2206
0
  }
2207
2208
0
  if (~file->RemoteApplicationMode)
2209
0
  {
2210
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteApplicationMode,
2211
0
                                   file->RemoteApplicationMode != 0))
2212
0
      return FALSE;
2213
0
  }
2214
2215
0
  if (~((size_t)file->RemoteApplicationProgram))
2216
0
  {
2217
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationProgram,
2218
0
                                     file->RemoteApplicationProgram))
2219
0
      return FALSE;
2220
0
  }
2221
2222
0
  if (~((size_t)file->RemoteApplicationName))
2223
0
  {
2224
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationName,
2225
0
                                     file->RemoteApplicationName))
2226
0
      return FALSE;
2227
0
  }
2228
2229
0
  if (~((size_t)file->RemoteApplicationIcon))
2230
0
  {
2231
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationIcon,
2232
0
                                     file->RemoteApplicationIcon))
2233
0
      return FALSE;
2234
0
  }
2235
2236
0
  if (~((size_t)file->RemoteApplicationFile))
2237
0
  {
2238
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationFile,
2239
0
                                     file->RemoteApplicationFile))
2240
0
      return FALSE;
2241
0
  }
2242
2243
0
  if (~((size_t)file->RemoteApplicationGuid))
2244
0
  {
2245
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationGuid,
2246
0
                                     file->RemoteApplicationGuid))
2247
0
      return FALSE;
2248
0
  }
2249
2250
0
  if (~((size_t)file->RemoteApplicationCmdLine))
2251
0
  {
2252
0
    if (!freerdp_settings_set_string(settings, FreeRDP_RemoteApplicationCmdLine,
2253
0
                                     file->RemoteApplicationCmdLine))
2254
0
      return FALSE;
2255
0
  }
2256
2257
0
  if (~file->SpanMonitors)
2258
0
  {
2259
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_SpanMonitors, file->SpanMonitors != 0))
2260
0
      return FALSE;
2261
0
  }
2262
2263
0
  if (~file->UseMultiMon)
2264
0
  {
2265
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_UseMultimon, file->UseMultiMon != 0))
2266
0
      return FALSE;
2267
0
  }
2268
2269
0
  if (~file->AllowFontSmoothing)
2270
0
  {
2271
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_AllowFontSmoothing,
2272
0
                                   file->AllowFontSmoothing != 0))
2273
0
      return FALSE;
2274
0
  }
2275
2276
0
  if (~file->DisableWallpaper)
2277
0
  {
2278
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DisableWallpaper,
2279
0
                                   file->DisableWallpaper != 0))
2280
0
      return FALSE;
2281
0
  }
2282
2283
0
  if (~file->DisableFullWindowDrag)
2284
0
  {
2285
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DisableFullWindowDrag,
2286
0
                                   file->DisableFullWindowDrag != 0))
2287
0
      return FALSE;
2288
0
  }
2289
2290
0
  if (~file->DisableMenuAnims)
2291
0
  {
2292
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DisableMenuAnims,
2293
0
                                   file->DisableMenuAnims != 0))
2294
0
      return FALSE;
2295
0
  }
2296
2297
0
  if (~file->DisableThemes)
2298
0
  {
2299
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DisableThemes, file->DisableThemes != 0))
2300
0
      return FALSE;
2301
0
  }
2302
2303
0
  if (~file->AllowDesktopComposition)
2304
0
  {
2305
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_AllowDesktopComposition,
2306
0
                                   file->AllowDesktopComposition != 0))
2307
0
      return FALSE;
2308
0
  }
2309
2310
0
  if (~file->BitmapCachePersistEnable)
2311
0
  {
2312
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_BitmapCachePersistEnabled,
2313
0
                                   file->BitmapCachePersistEnable != 0))
2314
0
      return FALSE;
2315
0
  }
2316
2317
0
  if (~file->DisableRemoteAppCapsCheck)
2318
0
  {
2319
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DisableRemoteAppCapsCheck,
2320
0
                                   file->DisableRemoteAppCapsCheck != 0))
2321
0
      return FALSE;
2322
0
  }
2323
2324
0
  if (~file->AutoReconnectionEnabled)
2325
0
  {
2326
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_AutoReconnectionEnabled,
2327
0
                                   file->AutoReconnectionEnabled != 0))
2328
0
      return FALSE;
2329
0
  }
2330
2331
0
  if (~file->AutoReconnectMaxRetries)
2332
0
  {
2333
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_AutoReconnectMaxRetries,
2334
0
                                     file->AutoReconnectMaxRetries))
2335
0
      return FALSE;
2336
0
  }
2337
2338
0
  if (~file->RedirectSmartCards)
2339
0
  {
2340
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSmartCards,
2341
0
                                   file->RedirectSmartCards != 0))
2342
0
      return FALSE;
2343
0
  }
2344
2345
0
  if (~file->RedirectWebauthN)
2346
0
  {
2347
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectWebAuthN,
2348
0
                                   file->RedirectWebauthN != 0))
2349
0
      return FALSE;
2350
0
  }
2351
2352
0
  if (~file->RedirectClipboard)
2353
0
  {
2354
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard,
2355
0
                                   file->RedirectClipboard != 0))
2356
0
      return FALSE;
2357
0
  }
2358
2359
0
  if (~file->RedirectPrinters)
2360
0
  {
2361
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters,
2362
0
                                   file->RedirectPrinters != 0))
2363
0
      return FALSE;
2364
0
  }
2365
2366
0
  if (~file->RedirectDrives)
2367
0
  {
2368
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, file->RedirectDrives != 0))
2369
0
      return FALSE;
2370
0
  }
2371
2372
0
  if (~file->RedirectPosDevices)
2373
0
  {
2374
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts,
2375
0
                                   file->RedirectComPorts != 0) ||
2376
0
        !freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts,
2377
0
                                   file->RedirectComPorts != 0))
2378
0
      return FALSE;
2379
0
  }
2380
2381
0
  if (~file->RedirectComPorts)
2382
0
  {
2383
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts,
2384
0
                                   file->RedirectComPorts != 0) ||
2385
0
        !freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts,
2386
0
                                   file->RedirectComPorts != 0))
2387
0
      return FALSE;
2388
0
  }
2389
2390
0
  if (~file->RedirectLocation && (file->RedirectLocation != 0))
2391
0
  {
2392
0
    size_t count = 0;
2393
2394
0
    char** ptr = CommandLineParseCommaSeparatedValuesEx(LOCATION_CHANNEL_NAME, NULL, &count);
2395
0
    const BOOL rc =
2396
0
        freerdp_client_add_dynamic_channel(settings, count, (const char* const*)ptr);
2397
0
    CommandLineParserFree(ptr);
2398
0
    if (!rc)
2399
0
      return FALSE;
2400
0
  }
2401
2402
0
  if (~file->RedirectDirectX)
2403
0
  {
2404
    /* What is this?! */
2405
0
  }
2406
2407
0
  if ((~((size_t)file->DevicesToRedirect)) && !utils_str_is_empty(file->DevicesToRedirect))
2408
0
  {
2409
    /**
2410
     * Devices to redirect:
2411
     * http://technet.microsoft.com/en-us/library/ff393728/
2412
     *
2413
     * This setting corresponds to the selections for Other supported Plug and Play
2414
     * (PnP) devices under More on the Local Resources tab under Options in RDC.
2415
     *
2416
     * Values:
2417
     *
2418
     * '*':
2419
     *  Redirect all supported Plug and Play devices.
2420
     *
2421
     * 'DynamicDevices':
2422
     *  Redirect any supported Plug and Play devices that are connected later.
2423
     *
2424
     * The hardware ID for the supported Plug and Play device:
2425
     *  Redirect the specified supported Plug and Play device.
2426
     *
2427
     * Examples:
2428
     *  devicestoredirect:s:*
2429
     *  devicestoredirect:s:DynamicDevices
2430
     *  devicestoredirect:s:USB\VID_04A9&PID_30C1\6&4BD985D&0&2;,DynamicDevices
2431
     *
2432
     */
2433
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DeviceRedirection, TRUE))
2434
0
      return FALSE;
2435
0
  }
2436
2437
0
  if ((~((size_t)file->DrivesToRedirect)) && !utils_str_is_empty(file->DrivesToRedirect))
2438
0
  {
2439
0
    if (!freerdp_settings_set_string(settings, FreeRDP_DrivesToRedirect,
2440
0
                                     file->DrivesToRedirect))
2441
0
      return FALSE;
2442
0
  }
2443
2444
0
  if ((~((size_t)file->RedirectCameras)) && !utils_str_is_empty(file->RedirectCameras))
2445
0
  {
2446
#if defined(CHANNEL_RDPECAM_CLIENT)
2447
    union
2448
    {
2449
      char** c;
2450
      const char* const* cc;
2451
    } cnv;
2452
    ADDIN_ARGV* args = rdp_file_to_args(RDPECAM_DVC_CHANNEL_NAME, file->RedirectCameras);
2453
    if (!args)
2454
      return FALSE;
2455
2456
    BOOL status = TRUE;
2457
    if (~file->EncodeRedirectedVideoCapture)
2458
    {
2459
      char encode[64] = WINPR_C_ARRAY_INIT;
2460
      (void)_snprintf(encode, sizeof(encode), "encode:%" PRIu32,
2461
                      file->EncodeRedirectedVideoCapture);
2462
      if (!freerdp_addin_argv_add_argument(args, encode))
2463
        status = FALSE;
2464
    }
2465
    if (~file->RedirectedVideoCaptureEncodingQuality)
2466
    {
2467
      char quality[64] = WINPR_C_ARRAY_INIT;
2468
      (void)_snprintf(quality, sizeof(quality), "quality:%" PRIu32,
2469
                      file->RedirectedVideoCaptureEncodingQuality);
2470
      if (!freerdp_addin_argv_add_argument(args, quality))
2471
        status = FALSE;
2472
    }
2473
2474
    cnv.c = args->argv;
2475
    if (status)
2476
      status = freerdp_client_add_dynamic_channel(
2477
          settings, WINPR_ASSERTING_INT_CAST(size_t, args->argc), cnv.cc);
2478
    freerdp_addin_argv_free(args);
2479
    if (!status)
2480
      return FALSE;
2481
#else
2482
0
    WLog_WARN(
2483
0
        TAG,
2484
0
        "This build does not support [MS-RDPECAM] camera redirection channel. Ignoring '%s'",
2485
0
        key_str_camerastoredirect);
2486
0
#endif
2487
0
  }
2488
2489
0
  if ((~((size_t)file->UsbDevicesToRedirect)) && !utils_str_is_empty(file->UsbDevicesToRedirect))
2490
0
  {
2491
#ifdef CHANNEL_URBDRC_CLIENT
2492
    union
2493
    {
2494
      char** c;
2495
      const char* const* cc;
2496
    } cnv;
2497
    ADDIN_ARGV* args = rdp_file_to_args(URBDRC_CHANNEL_NAME, file->UsbDevicesToRedirect);
2498
    if (!args)
2499
      return FALSE;
2500
    cnv.c = args->argv;
2501
    const BOOL status = freerdp_client_add_dynamic_channel(
2502
        settings, WINPR_ASSERTING_INT_CAST(size_t, args->argc), cnv.cc);
2503
    freerdp_addin_argv_free(args);
2504
    if (!status)
2505
      return FALSE;
2506
#else
2507
0
    WLog_WARN(TAG,
2508
0
              "This build does not support [MS-RDPEUSB] usb redirection channel. Ignoring '%s'",
2509
0
              key_str_usbdevicestoredirect);
2510
0
#endif
2511
0
  }
2512
2513
0
  if (~file->KeyboardHook)
2514
0
  {
2515
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_KeyboardHook, file->KeyboardHook))
2516
0
      return FALSE;
2517
0
  }
2518
2519
0
  if (~(size_t)file->SelectedMonitors)
2520
0
  {
2521
0
    size_t count = 0;
2522
0
    char** ptr = CommandLineParseCommaSeparatedValues(file->SelectedMonitors, &count);
2523
0
    UINT32* list = NULL;
2524
2525
0
    if (!freerdp_settings_set_pointer_len(settings, FreeRDP_MonitorIds, NULL, count))
2526
0
    {
2527
0
      CommandLineParserFree(ptr);
2528
0
      return FALSE;
2529
0
    }
2530
0
    list = freerdp_settings_get_pointer_writable(settings, FreeRDP_MonitorIds);
2531
0
    if (!list && (count > 0))
2532
0
    {
2533
0
      CommandLineParserFree(ptr);
2534
0
      return FALSE;
2535
0
    }
2536
0
    for (size_t x = 0; x < count; x++)
2537
0
    {
2538
0
      unsigned long val = 0;
2539
0
      errno = 0;
2540
0
      val = strtoul(ptr[x], NULL, 0);
2541
0
      if ((val >= UINT32_MAX) && (errno != 0))
2542
0
      {
2543
0
        CommandLineParserFree(ptr);
2544
0
        free(list);
2545
0
        return FALSE;
2546
0
      }
2547
0
      list[x] = (UINT32)val;
2548
0
    }
2549
0
    CommandLineParserFree(ptr);
2550
0
  }
2551
2552
0
  if (~file->DynamicResolution)
2553
0
  {
2554
0
    const BOOL val = file->DynamicResolution != 0;
2555
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_SupportDisplayControl, val))
2556
0
      return FALSE;
2557
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_DynamicResolutionUpdate, val))
2558
0
      return FALSE;
2559
0
  }
2560
2561
0
  if (~file->DesktopScaleFactor)
2562
0
  {
2563
0
    if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopScaleFactor,
2564
0
                                     file->DesktopScaleFactor))
2565
0
      return FALSE;
2566
0
  }
2567
2568
0
  if (~file->VideoPlaybackMode)
2569
0
  {
2570
0
    if (file->VideoPlaybackMode != 0)
2571
0
    {
2572
0
      if (!freerdp_settings_set_bool(settings, FreeRDP_SupportGeometryTracking, TRUE) ||
2573
0
          !freerdp_settings_set_bool(settings, FreeRDP_SupportVideoOptimized, TRUE))
2574
0
        return FALSE;
2575
0
    }
2576
0
    else
2577
0
    {
2578
0
      if (!freerdp_settings_set_bool(settings, FreeRDP_SupportVideoOptimized, FALSE))
2579
0
        return FALSE;
2580
0
    }
2581
0
  }
2582
  // TODO file->MaximizeToCurrentDisplays;
2583
  // TODO file->SingleMonInWindowedMode;
2584
  // TODO file->EncodeRedirectedVideoCapture;
2585
  // TODO file->RedirectedVideoCaptureEncodingQuality;
2586
2587
0
  if (~((size_t)file->PreconnectionBlob))
2588
0
  {
2589
0
    if (!freerdp_settings_set_string(settings, FreeRDP_PreconnectionBlob,
2590
0
                                     file->PreconnectionBlob) ||
2591
0
        !freerdp_settings_set_bool(settings, FreeRDP_SendPreconnectionPdu, TRUE))
2592
0
      return FALSE;
2593
0
  }
2594
2595
0
  if (~((size_t)file->KdcProxyName))
2596
0
  {
2597
0
    if (!freerdp_settings_set_string(settings, FreeRDP_KerberosKdcUrl, file->KdcProxyName))
2598
0
      return FALSE;
2599
0
  }
2600
2601
0
  if (~file->RdgIsKdcProxy)
2602
0
  {
2603
0
    if (!freerdp_settings_set_bool(settings, FreeRDP_KerberosRdgIsProxy,
2604
0
                                   file->RdgIsKdcProxy != 0))
2605
0
      return FALSE;
2606
0
  }
2607
2608
0
  if (file->args->argc > 1)
2609
0
  {
2610
0
    WCHAR* ConnectionFile =
2611
0
        freerdp_settings_get_string_as_utf16(settings, FreeRDP_ConnectionFile, NULL);
2612
2613
0
    if (freerdp_client_settings_parse_command_line(settings, file->args->argc, file->args->argv,
2614
0
                                                   FALSE) < 0)
2615
0
    {
2616
0
      free(ConnectionFile);
2617
0
      return FALSE;
2618
0
    }
2619
2620
0
    BOOL rc = freerdp_settings_set_string_from_utf16(settings, FreeRDP_ConnectionFile,
2621
0
                                                     ConnectionFile);
2622
0
    free(ConnectionFile);
2623
0
    if (!rc)
2624
0
      return FALSE;
2625
0
  }
2626
2627
0
  return TRUE;
2628
0
}
2629
2630
static BOOL freerdp_apply_connection_type_from_file(const rdpFile* file, rdpSettings* settings,
2631
                                                    UINT32 type)
2632
0
{
2633
0
  struct network_settings
2634
0
  {
2635
0
    FreeRDP_Settings_Keys_Bool id;
2636
0
    BOOL apply;
2637
0
    BOOL value[7];
2638
0
  };
2639
0
  WINPR_ASSERT(file);
2640
2641
0
  const struct network_settings config[] = { { FreeRDP_DisableWallpaper,
2642
0
                                             (~file->DisableWallpaper) == 0,
2643
0
                                             { TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE } },
2644
0
                                           { FreeRDP_AllowFontSmoothing,
2645
0
                                             (~file->AllowFontSmoothing) == 0,
2646
0
                                             { FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE } },
2647
0
                                           { FreeRDP_AllowDesktopComposition,
2648
0
                                             (~file->AllowDesktopComposition) == 0,
2649
0
                                             { FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE } },
2650
0
                                           { FreeRDP_DisableFullWindowDrag,
2651
0
                                             (~file->DisableFullWindowDrag) == 0,
2652
0
                                             { TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE } },
2653
0
                                           { FreeRDP_DisableMenuAnims,
2654
0
                                             (~file->DisableMenuAnims) == 0,
2655
0
                                             { TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE } },
2656
0
                                           { FreeRDP_DisableThemes,
2657
0
                                             (~file->DisableThemes) == 0,
2658
0
                                             { TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
2659
0
                                               FALSE } } };
2660
2661
0
  switch (type)
2662
0
  {
2663
0
    case CONNECTION_TYPE_INVALID:
2664
0
      return TRUE;
2665
2666
0
    case CONNECTION_TYPE_MODEM:
2667
0
    case CONNECTION_TYPE_BROADBAND_LOW:
2668
0
    case CONNECTION_TYPE_BROADBAND_HIGH:
2669
0
    case CONNECTION_TYPE_SATELLITE:
2670
0
    case CONNECTION_TYPE_WAN:
2671
0
    case CONNECTION_TYPE_LAN:
2672
0
    case CONNECTION_TYPE_AUTODETECT:
2673
0
      break;
2674
0
    default:
2675
0
      WLog_WARN(TAG, "Unknown ConnectionType %" PRIu32 ", aborting", type);
2676
0
      return FALSE;
2677
0
  }
2678
2679
0
  for (size_t x = 0; x < ARRAYSIZE(config); x++)
2680
0
  {
2681
0
    const struct network_settings* cur = &config[x];
2682
0
    if (!cur->apply)
2683
0
      continue;
2684
2685
0
    if (!freerdp_settings_set_bool(settings, cur->id, cur->value[type - 1]))
2686
0
      return FALSE;
2687
0
  }
2688
0
  return TRUE;
2689
0
}
2690
2691
static BOOL freerdp_set_connection_type_from_file(const rdpFile* file, rdpSettings* settings,
2692
                                                  UINT32 type)
2693
0
{
2694
0
  WINPR_ASSERT(file);
2695
0
  if (!freerdp_settings_set_uint32(settings, FreeRDP_ConnectionType, type))
2696
0
    return FALSE;
2697
2698
0
  switch (type)
2699
0
  {
2700
0
    case CONNECTION_TYPE_INVALID:
2701
0
    case CONNECTION_TYPE_MODEM:
2702
0
    case CONNECTION_TYPE_BROADBAND_LOW:
2703
0
    case CONNECTION_TYPE_SATELLITE:
2704
0
    case CONNECTION_TYPE_BROADBAND_HIGH:
2705
0
    case CONNECTION_TYPE_WAN:
2706
0
    case CONNECTION_TYPE_LAN:
2707
0
      if (!freerdp_apply_connection_type_from_file(file, settings, type))
2708
0
        return FALSE;
2709
0
      break;
2710
0
    case CONNECTION_TYPE_AUTODETECT:
2711
0
      if (!freerdp_apply_connection_type_from_file(file, settings, type))
2712
0
        return FALSE;
2713
      /* Automatically activate GFX and RFX codec support */
2714
#ifdef WITH_GFX_H264
2715
      if (!freerdp_settings_set_bool(settings, FreeRDP_GfxAVC444v2, TRUE) ||
2716
          !freerdp_settings_set_bool(settings, FreeRDP_GfxAVC444, TRUE) ||
2717
          !freerdp_settings_set_bool(settings, FreeRDP_GfxH264, TRUE))
2718
        return FALSE;
2719
#endif
2720
0
      if (!freerdp_settings_set_bool(settings, FreeRDP_RemoteFxCodec, TRUE) ||
2721
0
          !freerdp_settings_set_bool(settings, FreeRDP_SupportGraphicsPipeline, TRUE))
2722
0
        return FALSE;
2723
0
      break;
2724
0
    default:
2725
0
      WLog_WARN(TAG, "Unknown ConnectionType %" PRIu32 ", aborting", type);
2726
0
      return FALSE;
2727
0
  }
2728
2729
0
  return TRUE;
2730
0
}
2731
2732
BOOL freerdp_client_populate_settings_from_rdp_file(const rdpFile* file, rdpSettings* settings)
2733
0
{
2734
0
  if (!freerdp_client_populate_settings_from_rdp_file_unchecked(file, settings))
2735
0
    return FALSE;
2736
2737
0
  DWORD type = freerdp_settings_get_uint32(settings, FreeRDP_ConnectionType);
2738
0
  if ((~file->ConnectionType) == 0)
2739
0
  {
2740
0
    if (freerdp_settings_get_bool(settings, FreeRDP_NetworkAutoDetect))
2741
0
      type = CONNECTION_TYPE_AUTODETECT;
2742
0
  }
2743
0
  return freerdp_set_connection_type_from_file(file, settings, type);
2744
0
}
2745
2746
static rdpFileLine* freerdp_client_rdp_file_find_line_by_name(const rdpFile* file, const char* name)
2747
0
{
2748
0
  BOOL bFound = FALSE;
2749
0
  rdpFileLine* line = NULL;
2750
2751
0
  for (size_t index = 0; index < file->lineCount; index++)
2752
0
  {
2753
0
    line = &(file->lines[index]);
2754
2755
0
    if (line->flags & RDP_FILE_LINE_FLAG_FORMATTED)
2756
0
    {
2757
0
      if (_stricmp(name, line->name) == 0)
2758
0
      {
2759
0
        bFound = TRUE;
2760
0
        break;
2761
0
      }
2762
0
    }
2763
0
  }
2764
2765
0
  return (bFound) ? line : NULL;
2766
0
}
2767
/**
2768
 * Set a string option to a rdpFile
2769
 * @param file rdpFile
2770
 * @param name name of the option
2771
 * @param value value of the option
2772
 * @return 0 on success
2773
 */
2774
int freerdp_client_rdp_file_set_string_option(rdpFile* file, const char* name, const char* value)
2775
0
{
2776
0
  return freerdp_client_rdp_file_set_string(file, name, value);
2777
0
}
2778
2779
const char* freerdp_client_rdp_file_get_string_option(const rdpFile* file, const char* name)
2780
0
{
2781
0
  LPSTR* value = NULL;
2782
0
  rdpFileLine* line = NULL;
2783
2784
0
  rdpFile* wfile = WINPR_CAST_CONST_PTR_AWAY(file, rdpFile*);
2785
0
  if (freerdp_client_rdp_file_find_string_entry(wfile, name, &value, &line))
2786
0
  {
2787
0
    if (value && ~(size_t)(*value))
2788
0
      return *value;
2789
0
    if (line)
2790
0
      return line->sValue;
2791
0
  }
2792
2793
0
  return NULL;
2794
0
}
2795
2796
int freerdp_client_rdp_file_set_integer_option(rdpFile* file, const char* name, int value)
2797
0
{
2798
0
  return freerdp_client_rdp_file_set_integer(file, name, value);
2799
0
}
2800
2801
int freerdp_client_rdp_file_get_integer_option(const rdpFile* file, const char* name)
2802
0
{
2803
0
  DWORD* value = NULL;
2804
0
  rdpFileLine* line = NULL;
2805
2806
0
  rdpFile* wfile = WINPR_CAST_CONST_PTR_AWAY(file, rdpFile*);
2807
0
  if (freerdp_client_rdp_file_find_integer_entry(wfile, name, &value, &line))
2808
0
  {
2809
0
    if (value && ~(*value))
2810
0
      return WINPR_ASSERTING_INT_CAST(int, *value);
2811
0
    if (line)
2812
0
      return (int)line->iValue;
2813
0
  }
2814
2815
0
  return -1;
2816
0
}
2817
2818
static void freerdp_client_file_string_check_free(LPSTR str)
2819
0
{
2820
0
  if (~((size_t)str))
2821
0
    free(str);
2822
0
}
2823
2824
rdpFile* freerdp_client_rdp_file_new(void)
2825
0
{
2826
0
  return freerdp_client_rdp_file_new_ex(0);
2827
0
}
2828
2829
rdpFile* freerdp_client_rdp_file_new_ex(DWORD flags)
2830
0
{
2831
0
  rdpFile* file = (rdpFile*)calloc(1, sizeof(rdpFile));
2832
2833
0
  if (!file)
2834
0
    return NULL;
2835
2836
0
  file->flags = flags;
2837
2838
0
  FillMemory(file, sizeof(rdpFile), 0xFF);
2839
0
  file->lines = NULL;
2840
0
  file->lineCount = 0;
2841
0
  file->lineSize = 32;
2842
0
  file->GatewayProfileUsageMethod = 1;
2843
0
  file->lines = (rdpFileLine*)calloc(file->lineSize, sizeof(rdpFileLine));
2844
2845
0
  file->args = freerdp_addin_argv_new(0, NULL);
2846
0
  if (!file->lines || !file->args)
2847
0
    goto fail;
2848
2849
0
  if (!freerdp_client_add_option(file, "freerdp"))
2850
0
    goto fail;
2851
2852
0
  return file;
2853
0
fail:
2854
0
  WINPR_PRAGMA_DIAG_PUSH
2855
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2856
0
  freerdp_client_rdp_file_free(file);
2857
0
  WINPR_PRAGMA_DIAG_POP
2858
0
  return NULL;
2859
0
}
2860
void freerdp_client_rdp_file_free(rdpFile* file)
2861
0
{
2862
0
  if (file)
2863
0
  {
2864
0
    if (file->lineCount)
2865
0
    {
2866
0
      for (size_t i = 0; i < file->lineCount; i++)
2867
0
      {
2868
0
        free(file->lines[i].name);
2869
0
        free(file->lines[i].sValue);
2870
0
      }
2871
0
    }
2872
0
    free(file->lines);
2873
2874
0
    freerdp_addin_argv_free(file->args);
2875
2876
0
    freerdp_client_file_string_check_free(file->Username);
2877
0
    freerdp_client_file_string_check_free(file->Domain);
2878
0
    freerdp_client_file_string_check_free(file->Password);
2879
0
    freerdp_client_file_string_check_free(file->FullAddress);
2880
0
    freerdp_client_file_string_check_free(file->AlternateFullAddress);
2881
0
    freerdp_client_file_string_check_free(file->UsbDevicesToRedirect);
2882
0
    freerdp_client_file_string_check_free(file->RedirectCameras);
2883
0
    freerdp_client_file_string_check_free(file->SelectedMonitors);
2884
0
    freerdp_client_file_string_check_free(file->LoadBalanceInfo);
2885
0
    freerdp_client_file_string_check_free(file->RemoteApplicationName);
2886
0
    freerdp_client_file_string_check_free(file->RemoteApplicationIcon);
2887
0
    freerdp_client_file_string_check_free(file->RemoteApplicationProgram);
2888
0
    freerdp_client_file_string_check_free(file->RemoteApplicationFile);
2889
0
    freerdp_client_file_string_check_free(file->RemoteApplicationGuid);
2890
0
    freerdp_client_file_string_check_free(file->RemoteApplicationCmdLine);
2891
0
    freerdp_client_file_string_check_free(file->AlternateShell);
2892
0
    freerdp_client_file_string_check_free(file->ShellWorkingDirectory);
2893
0
    freerdp_client_file_string_check_free(file->GatewayHostname);
2894
0
    freerdp_client_file_string_check_free(file->GatewayAccessToken);
2895
0
    freerdp_client_file_string_check_free(file->KdcProxyName);
2896
0
    freerdp_client_file_string_check_free(file->DrivesToRedirect);
2897
0
    freerdp_client_file_string_check_free(file->DevicesToRedirect);
2898
0
    freerdp_client_file_string_check_free(file->WinPosStr);
2899
0
    freerdp_client_file_string_check_free(file->ResourceProvider);
2900
0
    freerdp_client_file_string_check_free(file->WvdEndpointPool);
2901
0
    freerdp_client_file_string_check_free(file->geo);
2902
0
    freerdp_client_file_string_check_free(file->armpath);
2903
0
    freerdp_client_file_string_check_free(file->aadtenantid);
2904
0
    freerdp_client_file_string_check_free(file->diagnosticserviceurl);
2905
0
    freerdp_client_file_string_check_free(file->hubdiscoverygeourl);
2906
0
    freerdp_client_file_string_check_free(file->activityhint);
2907
0
    free(file);
2908
0
  }
2909
0
}
2910
2911
void freerdp_client_rdp_file_set_callback_context(rdpFile* file, void* context)
2912
0
{
2913
0
  file->context = context;
2914
0
}