Coverage Report

Created: 2026-03-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/utils.c
Line
Count
Source
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 = nullptr;
44
0
  if (!value)
45
0
    return TRUE;
46
47
0
  (*dst) = _strdup(value);
48
0
  return (*dst) != nullptr;
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 = nullptr;
69
0
  rdpSettings* origSettings = nullptr;
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
  {
92
0
    if (!utils_sync_credentials(settings, FALSE))
93
0
      return AUTH_FAILED;
94
0
    return AUTH_SKIP;
95
0
  }
96
97
0
  if (!instance->GatewayAuthenticate && !instance->AuthenticateEx)
98
0
    return AUTH_NO_CREDENTIALS;
99
100
0
  if (!instance->GatewayAuthenticate)
101
0
  {
102
0
    proceed =
103
0
        instance->AuthenticateEx(instance, &settings->GatewayUsername,
104
0
                                 &settings->GatewayPassword, &settings->GatewayDomain, reason);
105
0
    if (!proceed)
106
0
      return AUTH_CANCELLED;
107
0
  }
108
0
  else
109
0
  {
110
0
    proceed =
111
0
        instance->GatewayAuthenticate(instance, &settings->GatewayUsername,
112
0
                                      &settings->GatewayPassword, &settings->GatewayDomain);
113
0
    if (!proceed)
114
0
      return AUTH_CANCELLED;
115
0
  }
116
117
0
  if (utils_str_is_empty(settings->GatewayUsername) ||
118
0
      utils_str_is_empty(settings->GatewayPassword))
119
0
    return AUTH_NO_CREDENTIALS;
120
121
0
  if (!utils_sync_credentials(settings, FALSE))
122
0
    return AUTH_FAILED;
123
124
  /* update original settings with provided user credentials */
125
0
  if (!utils_str_copy(settings->GatewayUsername, &origSettings->GatewayUsername))
126
0
    return AUTH_FAILED;
127
0
  if (!utils_str_copy(settings->GatewayDomain, &origSettings->GatewayDomain))
128
0
    return AUTH_FAILED;
129
0
  if (!utils_str_copy(settings->GatewayPassword, &origSettings->GatewayPassword))
130
0
    return AUTH_FAILED;
131
0
  if (!utils_sync_credentials(origSettings, FALSE))
132
0
    return AUTH_FAILED;
133
134
0
  if (!utils_copy_smartcard_settings(settings, origSettings))
135
0
    return AUTH_FAILED;
136
137
0
  return AUTH_SUCCESS;
138
0
}
139
140
auth_status utils_authenticate(freerdp* instance, rdp_auth_reason reason, BOOL override)
141
0
{
142
0
  rdpSettings* settings = nullptr;
143
0
  rdpSettings* origSettings = nullptr;
144
0
  BOOL prompt = !override;
145
0
  BOOL proceed = 0;
146
147
0
  WINPR_ASSERT(instance);
148
0
  WINPR_ASSERT(instance->context);
149
0
  WINPR_ASSERT(instance->context->settings);
150
0
  WINPR_ASSERT(instance->context->rdp);
151
0
  WINPR_ASSERT(instance->context->rdp->originalSettings);
152
153
0
  settings = instance->context->settings;
154
0
  origSettings = instance->context->rdp->originalSettings;
155
156
0
  if (freerdp_shall_disconnect_context(instance->context))
157
0
    return AUTH_FAILED;
158
159
0
  if (settings->ConnectChildSession)
160
0
    return AUTH_NO_CREDENTIALS;
161
162
  /* Ask for auth data if no or an empty username was specified or no password was given */
163
0
  if (utils_str_is_empty(freerdp_settings_get_string(settings, FreeRDP_Username)) ||
164
0
      (settings->Password == nullptr && settings->RedirectionPassword == nullptr))
165
0
    prompt = TRUE;
166
167
0
  if (!prompt)
168
0
    return AUTH_SKIP;
169
170
0
  switch (reason)
171
0
  {
172
0
    case AUTH_RDP:
173
0
    case AUTH_TLS:
174
0
      if (settings->SmartcardLogon)
175
0
      {
176
0
        if (!utils_str_is_empty(settings->Password))
177
0
        {
178
0
          WLog_INFO(TAG, "Authentication via smartcard");
179
0
          return AUTH_SUCCESS;
180
0
        }
181
0
        reason = AUTH_SMARTCARD_PIN;
182
0
      }
183
0
      break;
184
0
    case AUTH_NLA:
185
0
      if (settings->SmartcardLogon)
186
0
        reason = AUTH_SMARTCARD_PIN;
187
0
      break;
188
0
    case AUTH_RDSTLS:
189
0
    default:
190
0
      break;
191
0
  }
192
193
  /* If no callback is specified still continue connection */
194
0
  if (!instance->Authenticate && !instance->AuthenticateEx)
195
0
    return AUTH_NO_CREDENTIALS;
196
197
0
  if (!instance->Authenticate)
198
0
  {
199
0
    proceed = instance->AuthenticateEx(instance, &settings->Username, &settings->Password,
200
0
                                       &settings->Domain, reason);
201
0
    if (!proceed)
202
0
      return AUTH_CANCELLED;
203
0
  }
204
0
  else
205
0
  {
206
0
    proceed = instance->Authenticate(instance, &settings->Username, &settings->Password,
207
0
                                     &settings->Domain);
208
0
    if (!proceed)
209
0
      return AUTH_NO_CREDENTIALS;
210
0
  }
211
212
0
  if (utils_str_is_empty(settings->Username) || utils_str_is_empty(settings->Password))
213
0
    return AUTH_NO_CREDENTIALS;
214
215
0
  if (!utils_sync_credentials(settings, TRUE))
216
0
    return AUTH_FAILED;
217
218
  /* update original settings with provided user credentials */
219
0
  if (!utils_str_copy(settings->Username, &origSettings->Username))
220
0
    return AUTH_FAILED;
221
0
  if (!utils_str_copy(settings->Domain, &origSettings->Domain))
222
0
    return AUTH_FAILED;
223
0
  if (!utils_str_copy(settings->Password, &origSettings->Password))
224
0
    return AUTH_FAILED;
225
0
  if (!utils_sync_credentials(origSettings, TRUE))
226
0
    return AUTH_FAILED;
227
228
0
  if (!utils_copy_smartcard_settings(settings, origSettings))
229
0
    return AUTH_FAILED;
230
231
0
  return AUTH_SUCCESS;
232
0
}
233
234
BOOL utils_sync_credentials(rdpSettings* settings, BOOL toGateway)
235
0
{
236
0
  WINPR_ASSERT(settings);
237
0
  if (!settings->GatewayUseSameCredentials)
238
0
    return TRUE;
239
240
0
  if (toGateway)
241
0
  {
242
0
    if (!utils_str_copy(settings->Username, &settings->GatewayUsername))
243
0
      return FALSE;
244
0
    if (!utils_str_copy(settings->Domain, &settings->GatewayDomain))
245
0
      return FALSE;
246
0
    if (!utils_str_copy(settings->Password, &settings->GatewayPassword))
247
0
      return FALSE;
248
0
  }
249
0
  else
250
0
  {
251
0
    if (!utils_str_copy(settings->GatewayUsername, &settings->Username))
252
0
      return FALSE;
253
0
    if (!utils_str_copy(settings->GatewayDomain, &settings->Domain))
254
0
      return FALSE;
255
0
    if (!utils_str_copy(settings->GatewayPassword, &settings->Password))
256
0
      return FALSE;
257
0
  }
258
0
  return TRUE;
259
0
}
260
261
BOOL utils_persist_credentials(rdpSettings* settings, const rdpSettings* current)
262
0
{
263
0
  if (!settings || !current)
264
0
    return FALSE;
265
266
0
  const SSIZE_T keys[] = { FreeRDP_GatewayUsername, FreeRDP_GatewayDomain,
267
0
                         FreeRDP_GatewayPassword, FreeRDP_Username,
268
0
                         FreeRDP_Domain,          FreeRDP_Password };
269
270
0
  for (size_t x = 0; x < ARRAYSIZE(keys); x++)
271
0
  {
272
0
    const SSIZE_T key = keys[x];
273
0
    if (!freerdp_settings_copy_item(settings, current, key))
274
0
    {
275
0
      WLog_ERR(TAG, "Failed to copy %s from current to backup settings",
276
0
               freerdp_settings_get_name_for_key(key));
277
0
      return FALSE;
278
0
    }
279
0
  }
280
281
0
  return TRUE;
282
0
}
283
284
BOOL utils_str_is_empty(const char* str)
285
0
{
286
0
  if (!str)
287
0
    return TRUE;
288
0
  if (*str == '\0')
289
0
    return TRUE;
290
0
  return FALSE;
291
0
}
292
293
BOOL utils_abort_connect(rdpRdp* rdp)
294
940
{
295
940
  if (!rdp)
296
0
    return FALSE;
297
298
940
  return SetEvent(rdp->abortEvent);
299
940
}
300
301
BOOL utils_reset_abort(rdpRdp* rdp)
302
0
{
303
0
  WINPR_ASSERT(rdp);
304
305
0
  return ResetEvent(rdp->abortEvent);
306
0
}
307
308
HANDLE utils_get_abort_event(rdpRdp* rdp)
309
15.9k
{
310
15.9k
  WINPR_ASSERT(rdp);
311
15.9k
  return rdp->abortEvent;
312
15.9k
}
313
314
BOOL utils_abort_event_is_set(const rdpRdp* rdp)
315
74
{
316
74
  DWORD status = 0;
317
74
  WINPR_ASSERT(rdp);
318
74
  status = WaitForSingleObject(rdp->abortEvent, 0);
319
74
  return status == WAIT_OBJECT_0;
320
74
}
321
322
const char* utils_is_vsock(const char* hostname)
323
0
{
324
0
  if (!hostname)
325
0
    return nullptr;
326
327
0
  const char vsock[8] = { 'v', 's', 'o', 'c', 'k', ':', '/', '/' };
328
0
  if (strncmp(hostname, vsock, sizeof(vsock)) == 0)
329
0
    return &hostname[sizeof(vsock)];
330
0
  return nullptr;
331
0
}
332
333
static BOOL remove_rdpdr_type(rdpSettings* settings, UINT32 type)
334
0
{
335
0
  BOOL rc = TRUE;
336
0
  RDPDR_DEVICE* printer = nullptr;
337
0
  do
338
0
  {
339
0
    printer = freerdp_device_collection_find_type(settings, type);
340
0
    if (printer)
341
0
    {
342
0
      if (!freerdp_device_collection_del(settings, printer))
343
0
        rc = FALSE;
344
0
    }
345
0
    freerdp_device_free(printer);
346
0
  } while (printer);
347
0
  return rc;
348
0
}
349
350
static BOOL disable_clipboard(rdpSettings* settings)
351
0
{
352
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard, FALSE))
353
0
    return FALSE;
354
0
  freerdp_static_channel_collection_del(settings, CLIPRDR_SVC_CHANNEL_NAME);
355
0
  return TRUE;
356
0
}
357
358
static BOOL disable_drive(rdpSettings* settings)
359
0
{
360
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, FALSE))
361
0
    return FALSE;
362
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectHomeDrive, FALSE))
363
0
    return FALSE;
364
365
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_FILESYSTEM);
366
0
}
367
368
static BOOL disable_printers(rdpSettings* settings)
369
0
{
370
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters, FALSE))
371
0
    return FALSE;
372
373
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PRINT);
374
0
}
375
376
static BOOL disable_port(rdpSettings* settings)
377
0
{
378
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts, FALSE))
379
0
    return FALSE;
380
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts, FALSE))
381
0
    return FALSE;
382
0
  if (!remove_rdpdr_type(settings, RDPDR_DTYP_SERIAL))
383
0
    return FALSE;
384
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PARALLEL);
385
0
}
386
387
static BOOL disable_pnp(WINPR_ATTR_UNUSED rdpSettings* settings)
388
0
{
389
  // TODO(akallabeth): [MS-RDPEPNP] related stuff is disabled.
390
0
  return TRUE;
391
0
}
392
393
static BOOL apply_gw_policy(rdpContext* context)
394
0
{
395
0
  WINPR_ASSERT(context);
396
0
  return utils_reload_channels(context);
397
0
}
398
399
BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module)
400
0
{
401
0
  WINPR_ASSERT(log);
402
0
  WINPR_ASSERT(context);
403
404
0
  rdpSettings* settings = context->settings;
405
0
  WINPR_ASSERT(settings);
406
407
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
408
0
  {
409
0
    WLog_Print(log, WLOG_DEBUG, "[%s] policy allows all redirections", module);
410
0
  }
411
0
  else if (freerdp_settings_get_bool(settings, FreeRDP_GatewayIgnoreRedirectionPolicy))
412
0
  {
413
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
414
0
    WLog_Print(log, WLOG_INFO, "[%s] policy ignored on user request %s", module,
415
0
               utils_redir_flags_to_string(flags, buffer, sizeof(buffer)));
416
0
  }
417
0
  else if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
418
0
  {
419
0
    WLog_Print(log, WLOG_INFO, "[%s] policy denies all redirections", module);
420
0
    if (!disable_drive(settings))
421
0
      return FALSE;
422
0
    if (!disable_printers(settings))
423
0
      return FALSE;
424
0
    if (!disable_clipboard(settings))
425
0
      return FALSE;
426
0
    if (!disable_port(settings))
427
0
      return FALSE;
428
0
    if (!disable_pnp(settings))
429
0
      return FALSE;
430
0
    if (!apply_gw_policy(context))
431
0
      return FALSE;
432
0
  }
433
0
  else
434
0
  {
435
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
436
0
    {
437
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies drive redirections", module);
438
0
      if (!disable_drive(settings))
439
0
        return FALSE;
440
0
    }
441
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
442
0
    {
443
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies printer redirections", module);
444
0
      if (!disable_printers(settings))
445
0
        return FALSE;
446
0
    }
447
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
448
0
    {
449
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies port redirections", module);
450
0
      if (!disable_port(settings))
451
0
        return FALSE;
452
0
    }
453
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
454
0
    {
455
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies clipboard redirections", module);
456
0
      if (!disable_clipboard(settings))
457
0
        return FALSE;
458
0
    }
459
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
460
0
    {
461
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies PNP redirections", module);
462
0
      if (!disable_pnp(settings))
463
0
        return FALSE;
464
0
    }
465
0
    if (flags != 0)
466
0
    {
467
0
      if (!apply_gw_policy(context))
468
0
        return FALSE;
469
0
    }
470
0
  }
471
0
  return TRUE;
472
0
}
473
474
char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size)
475
0
{
476
0
  winpr_str_append("{", buffer, size, "");
477
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
478
0
    winpr_str_append("ENABLE_ALL", buffer, size, "|");
479
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
480
0
    winpr_str_append("DISABLE_ALL", buffer, size, "|");
481
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
482
0
    winpr_str_append("DISABLE_DRIVE", buffer, size, "|");
483
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
484
0
    winpr_str_append("DISABLE_PRINTER", buffer, size, "|");
485
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
486
0
    winpr_str_append("DISABLE_PORT", buffer, size, "|");
487
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
488
0
    winpr_str_append("DISABLE_CLIPBOARD", buffer, size, "|");
489
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
490
0
    winpr_str_append("DISABLE_PNP", buffer, size, "|");
491
492
0
  char fbuffer[16] = WINPR_C_ARRAY_INIT;
493
0
  (void)_snprintf(fbuffer, sizeof(fbuffer), "[0x%08" PRIx32 "]", flags);
494
495
0
  winpr_str_append(fbuffer, buffer, size, " ");
496
0
  winpr_str_append("{", buffer, size, "}");
497
0
  return buffer;
498
0
}
499
500
BOOL utils_reload_channels(rdpContext* context)
501
0
{
502
0
  WINPR_ASSERT(context);
503
504
0
  if (context->channels)
505
0
  {
506
0
    freerdp_channels_disconnect(context->channels, context->instance);
507
0
    freerdp_channels_close(context->channels, context->instance);
508
0
    freerdp_channels_free(context->channels);
509
0
  }
510
511
0
  context->channels = freerdp_channels_new(context->instance);
512
0
  if (!context->channels)
513
0
    return FALSE;
514
515
0
  freerdp_channels_register_instance(context->channels, context->instance);
516
517
0
  BOOL rc = TRUE;
518
0
  IFCALLRET(context->instance->LoadChannels, rc, context->instance);
519
0
  if (rc)
520
0
    return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK;
521
0
  return rc;
522
0
}