Coverage Report

Created: 2026-09-14 06:32

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