Coverage Report

Created: 2026-02-26 06:54

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 = 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
    case AUTH_RDSTLS:
185
0
    default:
186
0
      break;
187
0
  }
188
189
  /* If no callback is specified still continue connection */
190
0
  if (!instance->Authenticate && !instance->AuthenticateEx)
191
0
    return AUTH_NO_CREDENTIALS;
192
193
0
  if (!instance->Authenticate)
194
0
  {
195
0
    proceed = instance->AuthenticateEx(instance, &settings->Username, &settings->Password,
196
0
                                       &settings->Domain, reason);
197
0
    if (!proceed)
198
0
      return AUTH_CANCELLED;
199
0
  }
200
0
  else
201
0
  {
202
0
    proceed = instance->Authenticate(instance, &settings->Username, &settings->Password,
203
0
                                     &settings->Domain);
204
0
    if (!proceed)
205
0
      return AUTH_NO_CREDENTIALS;
206
0
  }
207
208
0
  if (utils_str_is_empty(settings->Username) || utils_str_is_empty(settings->Password))
209
0
    return AUTH_NO_CREDENTIALS;
210
211
0
  if (!utils_sync_credentials(settings, TRUE))
212
0
    return AUTH_FAILED;
213
214
  /* update original settings with provided user credentials */
215
0
  if (!utils_str_copy(settings->Username, &origSettings->Username))
216
0
    return AUTH_FAILED;
217
0
  if (!utils_str_copy(settings->Domain, &origSettings->Domain))
218
0
    return AUTH_FAILED;
219
0
  if (!utils_str_copy(settings->Password, &origSettings->Password))
220
0
    return AUTH_FAILED;
221
0
  if (!utils_sync_credentials(origSettings, TRUE))
222
0
    return AUTH_FAILED;
223
224
0
  if (!utils_copy_smartcard_settings(settings, origSettings))
225
0
    return AUTH_FAILED;
226
227
0
  return AUTH_SUCCESS;
228
0
}
229
230
BOOL utils_sync_credentials(rdpSettings* settings, BOOL toGateway)
231
0
{
232
0
  WINPR_ASSERT(settings);
233
0
  if (!settings->GatewayUseSameCredentials)
234
0
    return TRUE;
235
236
0
  if (toGateway)
237
0
  {
238
0
    if (!utils_str_copy(settings->Username, &settings->GatewayUsername))
239
0
      return FALSE;
240
0
    if (!utils_str_copy(settings->Domain, &settings->GatewayDomain))
241
0
      return FALSE;
242
0
    if (!utils_str_copy(settings->Password, &settings->GatewayPassword))
243
0
      return FALSE;
244
0
  }
245
0
  else
246
0
  {
247
0
    if (!utils_str_copy(settings->GatewayUsername, &settings->Username))
248
0
      return FALSE;
249
0
    if (!utils_str_copy(settings->GatewayDomain, &settings->Domain))
250
0
      return FALSE;
251
0
    if (!utils_str_copy(settings->GatewayPassword, &settings->Password))
252
0
      return FALSE;
253
0
  }
254
0
  return TRUE;
255
0
}
256
257
BOOL utils_persist_credentials(rdpSettings* settings, const rdpSettings* current)
258
0
{
259
0
  if (!settings || !current)
260
0
    return FALSE;
261
262
0
  const SSIZE_T keys[] = { FreeRDP_GatewayUsername, FreeRDP_GatewayDomain,
263
0
                         FreeRDP_GatewayPassword, FreeRDP_Username,
264
0
                         FreeRDP_Domain,          FreeRDP_Password };
265
266
0
  for (size_t x = 0; x < ARRAYSIZE(keys); x++)
267
0
  {
268
0
    const SSIZE_T key = keys[x];
269
0
    if (!freerdp_settings_copy_item(settings, current, key))
270
0
    {
271
0
      WLog_ERR(TAG, "Failed to copy %s from current to backup settings",
272
0
               freerdp_settings_get_name_for_key(key));
273
0
      return FALSE;
274
0
    }
275
0
  }
276
277
0
  return TRUE;
278
0
}
279
280
BOOL utils_str_is_empty(const char* str)
281
0
{
282
0
  if (!str)
283
0
    return TRUE;
284
0
  if (*str == '\0')
285
0
    return TRUE;
286
0
  return FALSE;
287
0
}
288
289
BOOL utils_abort_connect(rdpRdp* rdp)
290
944
{
291
944
  if (!rdp)
292
0
    return FALSE;
293
294
944
  return SetEvent(rdp->abortEvent);
295
944
}
296
297
BOOL utils_reset_abort(rdpRdp* rdp)
298
0
{
299
0
  WINPR_ASSERT(rdp);
300
301
0
  return ResetEvent(rdp->abortEvent);
302
0
}
303
304
HANDLE utils_get_abort_event(rdpRdp* rdp)
305
14.4k
{
306
14.4k
  WINPR_ASSERT(rdp);
307
14.4k
  return rdp->abortEvent;
308
14.4k
}
309
310
BOOL utils_abort_event_is_set(const rdpRdp* rdp)
311
64
{
312
64
  DWORD status = 0;
313
64
  WINPR_ASSERT(rdp);
314
64
  status = WaitForSingleObject(rdp->abortEvent, 0);
315
64
  return status == WAIT_OBJECT_0;
316
64
}
317
318
const char* utils_is_vsock(const char* hostname)
319
0
{
320
0
  if (!hostname)
321
0
    return NULL;
322
323
0
  const char vsock[8] = { 'v', 's', 'o', 'c', 'k', ':', '/', '/' };
324
0
  if (strncmp(hostname, vsock, sizeof(vsock)) == 0)
325
0
    return &hostname[sizeof(vsock)];
326
0
  return NULL;
327
0
}
328
329
static BOOL remove_rdpdr_type(rdpSettings* settings, UINT32 type)
330
0
{
331
0
  BOOL rc = TRUE;
332
0
  RDPDR_DEVICE* printer = NULL;
333
0
  do
334
0
  {
335
0
    printer = freerdp_device_collection_find_type(settings, type);
336
0
    if (printer)
337
0
    {
338
0
      if (!freerdp_device_collection_del(settings, printer))
339
0
        rc = FALSE;
340
0
    }
341
0
    freerdp_device_free(printer);
342
0
  } while (printer);
343
0
  return rc;
344
0
}
345
346
static BOOL disable_clipboard(rdpSettings* settings)
347
0
{
348
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard, FALSE))
349
0
    return FALSE;
350
0
  freerdp_static_channel_collection_del(settings, CLIPRDR_SVC_CHANNEL_NAME);
351
0
  return TRUE;
352
0
}
353
354
static BOOL disable_drive(rdpSettings* settings)
355
0
{
356
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, FALSE))
357
0
    return FALSE;
358
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectHomeDrive, FALSE))
359
0
    return FALSE;
360
361
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_FILESYSTEM);
362
0
}
363
364
static BOOL disable_printers(rdpSettings* settings)
365
0
{
366
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters, FALSE))
367
0
    return FALSE;
368
369
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PRINT);
370
0
}
371
372
static BOOL disable_port(rdpSettings* settings)
373
0
{
374
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts, FALSE))
375
0
    return FALSE;
376
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts, FALSE))
377
0
    return FALSE;
378
0
  if (!remove_rdpdr_type(settings, RDPDR_DTYP_SERIAL))
379
0
    return FALSE;
380
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PARALLEL);
381
0
}
382
383
static BOOL disable_pnp(WINPR_ATTR_UNUSED rdpSettings* settings)
384
0
{
385
  // TODO(akallabeth): [MS-RDPEPNP] related stuff is disabled.
386
0
  return TRUE;
387
0
}
388
389
static BOOL apply_gw_policy(rdpContext* context)
390
0
{
391
0
  WINPR_ASSERT(context);
392
0
  return utils_reload_channels(context);
393
0
}
394
395
BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module)
396
0
{
397
0
  WINPR_ASSERT(log);
398
0
  WINPR_ASSERT(context);
399
400
0
  rdpSettings* settings = context->settings;
401
0
  WINPR_ASSERT(settings);
402
403
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
404
0
  {
405
0
    WLog_Print(log, WLOG_DEBUG, "[%s] policy allows all redirections", module);
406
0
  }
407
0
  else if (freerdp_settings_get_bool(settings, FreeRDP_GatewayIgnoreRedirectionPolicy))
408
0
  {
409
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
410
0
    WLog_Print(log, WLOG_INFO, "[%s] policy ignored on user request %s", module,
411
0
               utils_redir_flags_to_string(flags, buffer, sizeof(buffer)));
412
0
  }
413
0
  else if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
414
0
  {
415
0
    WLog_Print(log, WLOG_INFO, "[%s] policy denies all redirections", module);
416
0
    if (!disable_drive(settings))
417
0
      return FALSE;
418
0
    if (!disable_printers(settings))
419
0
      return FALSE;
420
0
    if (!disable_clipboard(settings))
421
0
      return FALSE;
422
0
    if (!disable_port(settings))
423
0
      return FALSE;
424
0
    if (!disable_pnp(settings))
425
0
      return FALSE;
426
0
    if (!apply_gw_policy(context))
427
0
      return FALSE;
428
0
  }
429
0
  else
430
0
  {
431
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
432
0
    {
433
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies drive redirections", module);
434
0
      if (!disable_drive(settings))
435
0
        return FALSE;
436
0
    }
437
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
438
0
    {
439
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies printer redirections", module);
440
0
      if (!disable_printers(settings))
441
0
        return FALSE;
442
0
    }
443
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
444
0
    {
445
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies port redirections", module);
446
0
      if (!disable_port(settings))
447
0
        return FALSE;
448
0
    }
449
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
450
0
    {
451
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies clipboard redirections", module);
452
0
      if (!disable_clipboard(settings))
453
0
        return FALSE;
454
0
    }
455
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
456
0
    {
457
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies PNP redirections", module);
458
0
      if (!disable_pnp(settings))
459
0
        return FALSE;
460
0
    }
461
0
    if (flags != 0)
462
0
    {
463
0
      if (!apply_gw_policy(context))
464
0
        return FALSE;
465
0
    }
466
0
  }
467
0
  return TRUE;
468
0
}
469
470
char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size)
471
0
{
472
0
  winpr_str_append("{", buffer, size, "");
473
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
474
0
    winpr_str_append("ENABLE_ALL", buffer, size, "|");
475
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
476
0
    winpr_str_append("DISABLE_ALL", buffer, size, "|");
477
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
478
0
    winpr_str_append("DISABLE_DRIVE", buffer, size, "|");
479
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
480
0
    winpr_str_append("DISABLE_PRINTER", buffer, size, "|");
481
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
482
0
    winpr_str_append("DISABLE_PORT", buffer, size, "|");
483
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
484
0
    winpr_str_append("DISABLE_CLIPBOARD", buffer, size, "|");
485
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
486
0
    winpr_str_append("DISABLE_PNP", buffer, size, "|");
487
488
0
  char fbuffer[16] = WINPR_C_ARRAY_INIT;
489
0
  (void)_snprintf(fbuffer, sizeof(fbuffer), "[0x%08" PRIx32 "]", flags);
490
491
0
  winpr_str_append(fbuffer, buffer, size, " ");
492
0
  winpr_str_append("{", buffer, size, "}");
493
0
  return buffer;
494
0
}
495
496
BOOL utils_reload_channels(rdpContext* context)
497
0
{
498
0
  WINPR_ASSERT(context);
499
500
0
  freerdp_channels_disconnect(context->channels, context->instance);
501
0
  freerdp_channels_close(context->channels, context->instance);
502
0
  freerdp_channels_free(context->channels);
503
0
  context->channels = freerdp_channels_new(context->instance);
504
0
  WINPR_ASSERT(context->channels);
505
0
  freerdp_channels_register_instance(context->channels, context->instance);
506
507
0
  BOOL rc = TRUE;
508
0
  IFCALLRET(context->instance->LoadChannels, rc, context->instance);
509
0
  if (rc)
510
0
    return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK;
511
0
  return rc;
512
0
}