Coverage Report

Created: 2025-10-10 06:50

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
1.00k
{
291
1.00k
  if (!rdp)
292
0
    return FALSE;
293
294
1.00k
  return SetEvent(rdp->abortEvent);
295
1.00k
}
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
17.4k
{
306
17.4k
  WINPR_ASSERT(rdp);
307
17.4k
  return rdp->abortEvent;
308
17.4k
}
309
310
BOOL utils_abort_event_is_set(const rdpRdp* rdp)
311
67
{
312
67
  DWORD status = 0;
313
67
  WINPR_ASSERT(rdp);
314
67
  status = WaitForSingleObject(rdp->abortEvent, 0);
315
67
  return status == WAIT_OBJECT_0;
316
67
}
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] = "vsock://";
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
  RDPDR_DEVICE* printer = NULL;
332
0
  do
333
0
  {
334
0
    printer = freerdp_device_collection_find_type(settings, type);
335
0
    freerdp_device_collection_del(settings, printer);
336
0
    freerdp_device_free(printer);
337
0
  } while (printer);
338
0
  return TRUE;
339
0
}
340
341
static BOOL disable_clipboard(rdpSettings* settings)
342
0
{
343
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard, FALSE))
344
0
    return FALSE;
345
0
  freerdp_static_channel_collection_del(settings, CLIPRDR_SVC_CHANNEL_NAME);
346
0
  return TRUE;
347
0
}
348
349
static BOOL disable_drive(rdpSettings* settings)
350
0
{
351
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectDrives, FALSE))
352
0
    return FALSE;
353
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectHomeDrive, FALSE))
354
0
    return FALSE;
355
356
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_FILESYSTEM);
357
0
}
358
359
static BOOL disable_printers(rdpSettings* settings)
360
0
{
361
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectPrinters, FALSE))
362
0
    return FALSE;
363
364
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PRINT);
365
0
}
366
367
static BOOL disable_port(rdpSettings* settings)
368
0
{
369
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectParallelPorts, FALSE))
370
0
    return FALSE;
371
0
  if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectSerialPorts, FALSE))
372
0
    return FALSE;
373
0
  if (!remove_rdpdr_type(settings, RDPDR_DTYP_SERIAL))
374
0
    return FALSE;
375
0
  return remove_rdpdr_type(settings, RDPDR_DTYP_PARALLEL);
376
0
}
377
378
static BOOL disable_pnp(WINPR_ATTR_UNUSED rdpSettings* settings)
379
0
{
380
  // TODO(akallabeth): [MS-RDPEPNP] related stuff is disabled.
381
0
  return TRUE;
382
0
}
383
384
static BOOL apply_gw_policy(rdpContext* context)
385
0
{
386
0
  WINPR_ASSERT(context);
387
0
  return utils_reload_channels(context);
388
0
}
389
390
BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags, const char* module)
391
0
{
392
0
  WINPR_ASSERT(log);
393
0
  WINPR_ASSERT(context);
394
395
0
  rdpSettings* settings = context->settings;
396
0
  WINPR_ASSERT(settings);
397
398
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
399
0
  {
400
0
    WLog_Print(log, WLOG_DEBUG, "[%s] policy allows all redirections", module);
401
0
  }
402
0
  else if (freerdp_settings_get_bool(settings, FreeRDP_GatewayIgnoreRedirectionPolicy))
403
0
  {
404
0
    char buffer[128] = { 0 };
405
0
    WLog_Print(log, WLOG_INFO, "[%s] policy ignored on user request %s", module,
406
0
               utils_redir_flags_to_string(flags, buffer, sizeof(buffer)));
407
0
  }
408
0
  else if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
409
0
  {
410
0
    WLog_Print(log, WLOG_INFO, "[%s] policy denies all redirections", module);
411
0
    if (!disable_drive(settings))
412
0
      return FALSE;
413
0
    if (!disable_printers(settings))
414
0
      return FALSE;
415
0
    if (!disable_clipboard(settings))
416
0
      return FALSE;
417
0
    if (!disable_port(settings))
418
0
      return FALSE;
419
0
    if (!disable_pnp(settings))
420
0
      return FALSE;
421
0
    if (!apply_gw_policy(context))
422
0
      return FALSE;
423
0
  }
424
0
  else
425
0
  {
426
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
427
0
    {
428
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies drive redirections", module);
429
0
      if (!disable_drive(settings))
430
0
        return FALSE;
431
0
    }
432
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
433
0
    {
434
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies printer redirections", module);
435
0
      if (!disable_printers(settings))
436
0
        return FALSE;
437
0
    }
438
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
439
0
    {
440
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies port redirections", module);
441
0
      if (!disable_port(settings))
442
0
        return FALSE;
443
0
    }
444
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
445
0
    {
446
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies clipboard redirections", module);
447
0
      if (!disable_clipboard(settings))
448
0
        return FALSE;
449
0
    }
450
0
    if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
451
0
    {
452
0
      WLog_Print(log, WLOG_INFO, "[%s] policy denies PNP redirections", module);
453
0
      if (!disable_pnp(settings))
454
0
        return FALSE;
455
0
    }
456
0
    if (flags != 0)
457
0
    {
458
0
      if (!apply_gw_policy(context))
459
0
        return FALSE;
460
0
    }
461
0
  }
462
0
  return TRUE;
463
0
}
464
465
char* utils_redir_flags_to_string(UINT32 flags, char* buffer, size_t size)
466
0
{
467
0
  winpr_str_append("{", buffer, size, "");
468
0
  if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
469
0
    winpr_str_append("ENABLE_ALL", buffer, size, "|");
470
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
471
0
    winpr_str_append("DISABLE_ALL", buffer, size, "|");
472
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
473
0
    winpr_str_append("DISABLE_DRIVE", buffer, size, "|");
474
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
475
0
    winpr_str_append("DISABLE_PRINTER", buffer, size, "|");
476
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
477
0
    winpr_str_append("DISABLE_PORT", buffer, size, "|");
478
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
479
0
    winpr_str_append("DISABLE_CLIPBOARD", buffer, size, "|");
480
0
  if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
481
0
    winpr_str_append("DISABLE_PNP", buffer, size, "|");
482
483
0
  char fbuffer[16] = { 0 };
484
0
  (void)_snprintf(fbuffer, sizeof(fbuffer), "[0x%08" PRIx32 "]", flags);
485
486
0
  winpr_str_append(fbuffer, buffer, size, " ");
487
0
  winpr_str_append("{", buffer, size, "}");
488
0
  return buffer;
489
0
}
490
491
BOOL utils_reload_channels(rdpContext* context)
492
0
{
493
0
  WINPR_ASSERT(context);
494
495
0
  freerdp_channels_disconnect(context->channels, context->instance);
496
0
  freerdp_channels_close(context->channels, context->instance);
497
0
  freerdp_channels_free(context->channels);
498
0
  context->channels = freerdp_channels_new(context->instance);
499
0
  WINPR_ASSERT(context->channels);
500
0
  freerdp_channels_register_instance(context->channels, context->instance);
501
502
0
  BOOL rc = TRUE;
503
0
  IFCALLRET(context->instance->LoadChannels, rc, context->instance);
504
0
  if (rc)
505
0
    return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK;
506
0
  return rc;
507
0
}