Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/core/utils.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Terminal Server Gateway (utils)
4
 *
5
 * Copyright 2021 Armin Novak <armin.novak@thincast.com>
6
 * Copyright 2021 Thincast Technologies GmbH
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
#include <freerdp/config.h>
22
23
#include "settings.h"
24
25
#include <winpr/assert.h>
26
27
#include <freerdp/freerdp.h>
28
#include <freerdp/channels/cliprdr.h>
29
#include <freerdp/channels/rdpdr.h>
30
31
#include <freerdp/log.h>
32
#define TAG FREERDP_TAG("core.gateway.utils")
33
34
#include "utils.h"
35
36
#include "../core/rdp.h"
37
38
BOOL utils_str_copy(const char* value, char** dst)
39
0
{
40
0
  WINPR_ASSERT(dst);
41
42
0
  free(*dst);
43
0
  *dst = NULL;
44
0
  if (!value)
45
0
    return TRUE;
46
47
0
  (*dst) = _strdup(value);
48
0
  return (*dst) != NULL;
49
0
}
50
51
static BOOL utils_copy_smartcard_settings(const rdpSettings* settings, rdpSettings* origSettings)
52
0
{
53
  /* update original settings with provided smart card settings */
54
0
  origSettings->SmartcardLogon = settings->SmartcardLogon;
55
0
  origSettings->PasswordIsSmartcardPin = settings->PasswordIsSmartcardPin;
56
0
  if (!utils_str_copy(settings->ReaderName, &origSettings->ReaderName))
57
0
    return FALSE;
58
0
  if (!utils_str_copy(settings->CspName, &origSettings->CspName))
59
0
    return FALSE;
60
0
  if (!utils_str_copy(settings->ContainerName, &origSettings->ContainerName))
61
0
    return FALSE;
62
63
0
  return TRUE;
64
0
}
65
66
auth_status utils_authenticate_gateway(freerdp* instance, rdp_auth_reason reason)
67
0
{
68
0
  rdpSettings* settings = NULL;
69
0
  rdpSettings* origSettings = NULL;
70
0
  BOOL prompt = FALSE;
71
0
  BOOL proceed = 0;
72
73
0
  WINPR_ASSERT(instance);
74
0
  WINPR_ASSERT(instance->context);
75
0
  WINPR_ASSERT(instance->context->settings);
76
0
  WINPR_ASSERT(instance->context->rdp);
77
0
  WINPR_ASSERT(instance->context->rdp->originalSettings);
78
79
0
  settings = instance->context->settings;
80
0
  origSettings = instance->context->rdp->originalSettings;
81
82
0
  if (freerdp_shall_disconnect_context(instance->context))
83
0
    return AUTH_FAILED;
84
85
0
  if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_GatewayPassword)))
86
0
    prompt = TRUE;
87
0
  if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_GatewayUsername)))
88
0
    prompt = TRUE;
89
90
0
  if (!prompt)
91
0
    return AUTH_SKIP;
92
93
0
  if (!instance->GatewayAuthenticate && !instance->AuthenticateEx)
94
0
    return AUTH_NO_CREDENTIALS;
95
96
0
  if (!instance->GatewayAuthenticate)
97
0
  {
98
0
    proceed =
99
0
        instance->AuthenticateEx(instance, &settings->GatewayUsername,
100
0
                                 &settings->GatewayPassword, &settings->GatewayDomain, reason);
101
0
    if (!proceed)
102
0
      return AUTH_CANCELLED;
103
0
  }
104
0
  else
105
0
  {
106
0
    proceed =
107
0
        instance->GatewayAuthenticate(instance, &settings->GatewayUsername,
108
0
                                      &settings->GatewayPassword, &settings->GatewayDomain);
109
0
    if (!proceed)
110
0
      return AUTH_CANCELLED;
111
0
  }
112
113
0
  if (utils_str_is_empty(settings->GatewayUsername) ||
114
0
      utils_str_is_empty(settings->GatewayPassword))
115
0
    return AUTH_NO_CREDENTIALS;
116
117
0
  if (!utils_sync_credentials(settings, FALSE))
118
0
    return AUTH_FAILED;
119
120
  /* update original settings with provided user credentials */
121
0
  if (!utils_str_copy(settings->GatewayUsername, &origSettings->GatewayUsername))
122
0
    return AUTH_FAILED;
123
0
  if (!utils_str_copy(settings->GatewayDomain, &origSettings->GatewayDomain))
124
0
    return AUTH_FAILED;
125
0
  if (!utils_str_copy(settings->GatewayPassword, &origSettings->GatewayPassword))
126
0
    return AUTH_FAILED;
127
0
  if (!utils_sync_credentials(origSettings, FALSE))
128
0
    return AUTH_FAILED;
129
130
0
  if (!utils_copy_smartcard_settings(settings, origSettings))
131
0
    return AUTH_FAILED;
132
133
0
  return AUTH_SUCCESS;
134
0
}
135
136
auth_status utils_authenticate(freerdp* instance, rdp_auth_reason reason, BOOL override)
137
0
{
138
0
  rdpSettings* settings = NULL;
139
0
  rdpSettings* origSettings = NULL;
140
0
  BOOL prompt = !override;
141
0
  BOOL proceed = 0;
142
143
0
  WINPR_ASSERT(instance);
144
0
  WINPR_ASSERT(instance->context);
145
0
  WINPR_ASSERT(instance->context->settings);
146
0
  WINPR_ASSERT(instance->context->rdp);
147
0
  WINPR_ASSERT(instance->context->rdp->originalSettings);
148
149
0
  settings = instance->context->settings;
150
0
  origSettings = instance->context->rdp->originalSettings;
151
152
0
  if (freerdp_shall_disconnect_context(instance->context))
153
0
    return AUTH_FAILED;
154
155
0
  if (settings->ConnectChildSession)
156
0
    return AUTH_NO_CREDENTIALS;
157
158
  /* Ask for auth data if no or an empty username was specified or no password was given */
159
0
  if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_Username)) ||
160
0
      (settings->Password == NULL && settings->RedirectionPassword == NULL))
161
0
    prompt = TRUE;
162
163
0
  if (!prompt)
164
0
    return AUTH_SKIP;
165
166
0
  switch (reason)
167
0
  {
168
0
    case AUTH_RDP:
169
0
    case AUTH_TLS:
170
0
      if (settings->SmartcardLogon)
171
0
      {
172
0
        if (!utils_str_is_empty(settings->Password))
173
0
        {
174
0
          WLog_INFO(TAG, "Authentication via smartcard");
175
0
          return AUTH_SUCCESS;
176
0
        }
177
0
        reason = AUTH_SMARTCARD_PIN;
178
0
      }
179
0
      break;
180
0
    case AUTH_NLA:
181
0
      if (settings->SmartcardLogon)
182
0
        reason = AUTH_SMARTCARD_PIN;
183
0
      break;
184
0
    default:
185
0
      break;
186
0
  }
187
188
  /* If no callback is specified still continue connection */
189
0
  if (!instance->Authenticate && !instance->AuthenticateEx)
190
0
    return AUTH_NO_CREDENTIALS;
191
192
0
  if (!instance->Authenticate)
193
0
  {
194
0
    proceed = instance->AuthenticateEx(instance, &settings->Username, &settings->Password,
195
0
                                       &settings->Domain, reason);
196
0
    if (!proceed)
197
0
      return AUTH_CANCELLED;
198
0
  }
199
0
  else
200
0
  {
201
0
    proceed = instance->Authenticate(instance, &settings->Username, &settings->Password,
202
0
                                     &settings->Domain);
203
0
    if (!proceed)
204
0
      return AUTH_NO_CREDENTIALS;
205
0
  }
206
207
0
  if (utils_str_is_empty(settings->Username) || utils_str_is_empty(settings->Password))
208
0
    return AUTH_NO_CREDENTIALS;
209
210
0
  if (!utils_sync_credentials(settings, TRUE))
211
0
    return AUTH_FAILED;
212
213
  /* update original settings with provided user credentials */
214
0
  if (!utils_str_copy(settings->Username, &origSettings->Username))
215
0
    return AUTH_FAILED;
216
0
  if (!utils_str_copy(settings->Domain, &origSettings->Domain))
217
0
    return AUTH_FAILED;
218
0
  if (!utils_str_copy(settings->Password, &origSettings->Password))
219
0
    return AUTH_FAILED;
220
0
  if (!utils_sync_credentials(origSettings, TRUE))
221
0
    return AUTH_FAILED;
222
223
0
  if (!utils_copy_smartcard_settings(settings, origSettings))
224
0
    return AUTH_FAILED;
225
226
0
  return AUTH_SUCCESS;
227
0
}
228
229
BOOL utils_sync_credentials(rdpSettings* settings, BOOL toGateway)
230
0
{
231
0
  WINPR_ASSERT(settings);
232
0
  if (!settings->GatewayUseSameCredentials)
233
0
    return TRUE;
234
235
0
  if (toGateway)
236
0
  {
237
0
    if (!utils_str_copy(settings->Username, &settings->GatewayUsername))
238
0
      return FALSE;
239
0
    if (!utils_str_copy(settings->Domain, &settings->GatewayDomain))
240
0
      return FALSE;
241
0
    if (!utils_str_copy(settings->Password, &settings->GatewayPassword))
242
0
      return FALSE;
243
0
  }
244
0
  else
245
0
  {
246
0
    if (!utils_str_copy(settings->GatewayUsername, &settings->Username))
247
0
      return FALSE;
248
0
    if (!utils_str_copy(settings->GatewayDomain, &settings->Domain))
249
0
      return FALSE;
250
0
    if (!utils_str_copy(settings->GatewayPassword, &settings->Password))
251
0
      return FALSE;
252
0
  }
253
0
  return TRUE;
254
0
}
255
256
BOOL utils_persist_credentials(rdpSettings* settings, const rdpSettings* current)
257
0
{
258
0
  if (!settings || !current)
259
0
    return FALSE;
260
261
0
  const SSIZE_T keys[] = { FreeRDP_GatewayUsername, FreeRDP_GatewayDomain,
262
0
                         FreeRDP_GatewayPassword, FreeRDP_Username,
263
0
                         FreeRDP_Domain,          FreeRDP_Password };
264
265
0
  for (size_t x = 0; x < ARRAYSIZE(keys); x++)
266
0
  {
267
0
    const SSIZE_T key = keys[x];
268
0
    if (!freerdp_settings_copy_item(settings, current, key))
269
0
    {
270
0
      WLog_ERR(TAG, "Failed to copy %s from current to backup settings",
271
0
               freerdp_settings_get_name_for_key(key));
272
0
      return FALSE;
273
0
    }
274
0
  }
275
276
0
  return TRUE;
277
0
}
278
279
BOOL utils_str_is_empty(const char* str)
280
0
{
281
0
  if (!str)
282
0
    return TRUE;
283
0
  if (*str == '\0')
284
0
    return TRUE;
285
0
  return FALSE;
286
0
}
287
288
BOOL utils_abort_connect(rdpRdp* rdp)
289
0
{
290
0
  if (!rdp)
291
0
    return FALSE;
292
293
0
  return SetEvent(rdp->abortEvent);
294
0
}
295
296
BOOL utils_reset_abort(rdpRdp* rdp)
297
0
{
298
0
  WINPR_ASSERT(rdp);
299
300
0
  return ResetEvent(rdp->abortEvent);
301
0
}
302
303
HANDLE utils_get_abort_event(rdpRdp* rdp)
304
0
{
305
0
  WINPR_ASSERT(rdp);
306
0
  return rdp->abortEvent;
307
0
}
308
309
BOOL utils_abort_event_is_set(const rdpRdp* rdp)
310
0
{
311
0
  DWORD status = 0;
312
0
  WINPR_ASSERT(rdp);
313
0
  status = WaitForSingleObject(rdp->abortEvent, 0);
314
0
  return status == WAIT_OBJECT_0;
315
0
}
316
317
const char* utils_is_vsock(const char* hostname)
318
0
{
319
0
  if (!hostname)
320
0
    return NULL;
321
322
0
  const char vsock[8] = "vsock://";
323
0
  if (strncmp(hostname, vsock, sizeof(vsock)) == 0)
324
0
    return &hostname[sizeof(vsock)];
325
0
  return NULL;
326
0
}
327
328
static BOOL remove_rdpdr_type(rdpSettings* settings, UINT32 type)
329
0
{
330
0
  RDPDR_DEVICE* printer = NULL;
331
0
  do
332
0
  {
333
0
    printer = freerdp_device_collection_find_type(settings, type);
334
0
    freerdp_device_collection_del(settings, printer);
335
0
    freerdp_device_free(printer);
336
0
  } while (printer);
337
0
  return TRUE;
338
0
}
339
340
static BOOL disable_clipboard(rdpSettings* settings)
341
0
{
342
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard, FALSE))
343
0
    return FALSE;
344
0
  freerdp_static_channel_collection_del(settings, CLIPRDR_SVC_CHANNEL_NAME);
345
0
  return TRUE;
346
0
}
347
348
static BOOL disable_drive(rdpSettings* settings)
349
0
{
350
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, FALSE))
351
0
    return FALSE;
352
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectHomeDrive, FALSE))
353
0
    return FALSE;
354
355
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_FILESYSTEM);
356
0
}
357
358
static BOOL disable_printers(rdpSettings* settings)
359
0
{
360
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters, FALSE))
361
0
    return FALSE;
362
363
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PRINT);
364
0
}
365
366
static BOOL disable_port(rdpSettings* settings)
367
0
{
368
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts, FALSE))
369
0
    return FALSE;
370
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts, FALSE))
371
0
    return FALSE;
372
0
  if (!remove_rdpdr_type(settings, RDPDR_DTYP_SERIAL))
373
0
    return FALSE;
374
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PARALLEL);
375
0
}
376
377
static BOOL disable_pnp(WINPR_ATTR_UNUSED rdpSettings* settings)
378
0
{
379
  // TODO(akallabeth): [MS-RDPEPNP] related stuff is disabled.
380
0
  return TRUE;
381
0
}
382
383
static BOOL apply_gw_policy(rdpContext* context)
384
0
{
385
0
  WINPR_ASSERT(context);
386
0
  return utils_reload_channels(context);
387
0
}
388
389
BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module)
390
0
{
391
0
  WINPR_ASSERT(log);
392
0
  WINPR_ASSERT(context);
393
394
0
  rdpSettings* settings = context->settings;
395
0
  WINPR_ASSERT(settings);
396
397
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
398
0
  {
399
0
    WLog_Print(log, WLOG_DEBUG, "[%s] policy allows all redirections", module);
400
0
  }
401
0
  else if (freerdp_settings_get_bool(settings, FreeRDP_GatewayIgnoreRedirectionPolicy))
402
0
  {
403
0
    char buffer[128] = { 0 };
404
0
    WLog_Print(log, WLOG_INFO, "[%s] policy ignored on user request %s", module,
405
0
               utils_redir_flags_to_string(flags, buffer, sizeof(buffer)));
406
0
  }
407
0
  else if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
408
0
  {
409
0
    WLog_Print(log, WLOG_INFO, "[%s] policy denies all redirections", module);
410
0
    if (!disable_drive(settings))
411
0
      return FALSE;
412
0
    if (!disable_printers(settings))
413
0
      return FALSE;
414
0
    if (!disable_clipboard(settings))
415
0
      return FALSE;
416
0
    if (!disable_port(settings))
417
0
      return FALSE;
418
0
    if (!disable_pnp(settings))
419
0
      return FALSE;
420
0
    if (!apply_gw_policy(context))
421
0
      return FALSE;
422
0
  }
423
0
  else
424
0
  {
425
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
426
0
    {
427
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies drive redirections", module);
428
0
      if (!disable_drive(settings))
429
0
        return FALSE;
430
0
    }
431
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
432
0
    {
433
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies printer redirections", module);
434
0
      if (!disable_printers(settings))
435
0
        return FALSE;
436
0
    }
437
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
438
0
    {
439
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies port redirections", module);
440
0
      if (!disable_port(settings))
441
0
        return FALSE;
442
0
    }
443
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
444
0
    {
445
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies clipboard redirections", module);
446
0
      if (!disable_clipboard(settings))
447
0
        return FALSE;
448
0
    }
449
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
450
0
    {
451
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies PNP redirections", module);
452
0
      if (!disable_pnp(settings))
453
0
        return FALSE;
454
0
    }
455
0
    if (flags != 0)
456
0
    {
457
0
      if (!apply_gw_policy(context))
458
0
        return FALSE;
459
0
    }
460
0
  }
461
0
  return TRUE;
462
0
}
463
464
char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size)
465
0
{
466
0
  winpr_str_append("{", buffer, size, "");
467
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
468
0
    winpr_str_append("ENABLE_ALL", buffer, size, "|");
469
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
470
0
    winpr_str_append("DISABLE_ALL", buffer, size, "|");
471
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
472
0
    winpr_str_append("DISABLE_DRIVE", buffer, size, "|");
473
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
474
0
    winpr_str_append("DISABLE_PRINTER", buffer, size, "|");
475
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
476
0
    winpr_str_append("DISABLE_PORT", buffer, size, "|");
477
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
478
0
    winpr_str_append("DISABLE_CLIPBOARD", buffer, size, "|");
479
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
480
0
    winpr_str_append("DISABLE_PNP", buffer, size, "|");
481
482
0
  char fbuffer[16] = { 0 };
483
0
  (void)_snprintf(fbuffer, sizeof(fbuffer), "[0x%08" PRIx32 "]", flags);
484
485
0
  winpr_str_append(fbuffer, buffer, size, " ");
486
0
  winpr_str_append("{", buffer, size, "}");
487
0
  return buffer;
488
0
}
489
490
BOOL utils_reload_channels(rdpContext* context)
491
0
{
492
0
  WINPR_ASSERT(context);
493
494
0
  freerdp_channels_disconnect(context->channels, context->instance);
495
0
  freerdp_channels_close(context->channels, context->instance);
496
0
  freerdp_channels_free(context->channels);
497
0
  context->channels = freerdp_channels_new(context->instance);
498
0
  WINPR_ASSERT(context->channels);
499
0
  freerdp_channels_register_instance(context->channels, context->instance);
500
501
0
  BOOL rc = TRUE;
502
0
  IFCALLRET(context->instance->LoadChannels, rc, context->instance);
503
0
  if (rc)
504
0
    return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK;
505
0
  return rc;
506
0
}