21 #include <freerdp/config.h>
25 #include <winpr/assert.h>
27 #include <freerdp/freerdp.h>
28 #include <freerdp/channels/cliprdr.h>
29 #include <freerdp/channels/rdpdr.h>
31 #include <freerdp/log.h>
32 #define TAG FREERDP_TAG("core.gateway.utils")
36 #include "../core/rdp.h"
38 BOOL utils_str_copy(
const char* value,
char** dst)
47 (*dst) = _strdup(value);
48 return (*dst) != NULL;
51 static BOOL utils_copy_smartcard_settings(
const rdpSettings* settings, rdpSettings* origSettings)
54 origSettings->SmartcardLogon = settings->SmartcardLogon;
55 origSettings->PasswordIsSmartcardPin = settings->PasswordIsSmartcardPin;
56 if (!utils_str_copy(settings->ReaderName, &origSettings->ReaderName))
58 if (!utils_str_copy(settings->CspName, &origSettings->CspName))
60 if (!utils_str_copy(settings->ContainerName, &origSettings->ContainerName))
66 auth_status utils_authenticate_gateway(freerdp* instance, rdp_auth_reason reason)
68 rdpSettings* settings = NULL;
69 rdpSettings* origSettings = NULL;
73 WINPR_ASSERT(instance);
74 WINPR_ASSERT(instance->context);
75 WINPR_ASSERT(instance->context->settings);
76 WINPR_ASSERT(instance->context->rdp);
77 WINPR_ASSERT(instance->context->rdp->originalSettings);
79 settings = instance->context->settings;
80 origSettings = instance->context->rdp->originalSettings;
82 if (freerdp_shall_disconnect_context(instance->context))
93 if (!instance->GatewayAuthenticate && !instance->AuthenticateEx)
94 return AUTH_NO_CREDENTIALS;
96 if (!instance->GatewayAuthenticate)
99 instance->AuthenticateEx(instance, &settings->GatewayUsername,
100 &settings->GatewayPassword, &settings->GatewayDomain, reason);
102 return AUTH_CANCELLED;
107 instance->GatewayAuthenticate(instance, &settings->GatewayUsername,
108 &settings->GatewayPassword, &settings->GatewayDomain);
110 return AUTH_CANCELLED;
113 if (utils_str_is_empty(settings->GatewayUsername) ||
114 utils_str_is_empty(settings->GatewayPassword))
115 return AUTH_NO_CREDENTIALS;
117 if (!utils_sync_credentials(settings, FALSE))
121 if (!utils_str_copy(settings->GatewayUsername, &origSettings->GatewayUsername))
123 if (!utils_str_copy(settings->GatewayDomain, &origSettings->GatewayDomain))
125 if (!utils_str_copy(settings->GatewayPassword, &origSettings->GatewayPassword))
127 if (!utils_sync_credentials(origSettings, FALSE))
130 if (!utils_copy_smartcard_settings(settings, origSettings))
136 auth_status utils_authenticate(freerdp* instance, rdp_auth_reason reason, BOOL
override)
138 rdpSettings* settings = NULL;
139 rdpSettings* origSettings = NULL;
140 BOOL prompt = !
override;
143 WINPR_ASSERT(instance);
144 WINPR_ASSERT(instance->context);
145 WINPR_ASSERT(instance->context->settings);
146 WINPR_ASSERT(instance->context->rdp);
147 WINPR_ASSERT(instance->context->rdp->originalSettings);
149 settings = instance->context->settings;
150 origSettings = instance->context->rdp->originalSettings;
152 if (freerdp_shall_disconnect_context(instance->context))
155 if (settings->ConnectChildSession)
156 return AUTH_NO_CREDENTIALS;
160 (settings->Password == NULL && settings->RedirectionPassword == NULL))
170 if (settings->SmartcardLogon)
172 if (!utils_str_is_empty(settings->Password))
174 WLog_INFO(TAG,
"Authentication via smartcard");
177 reason = AUTH_SMARTCARD_PIN;
181 if (settings->SmartcardLogon)
182 reason = AUTH_SMARTCARD_PIN;
189 if (!instance->Authenticate && !instance->AuthenticateEx)
190 return AUTH_NO_CREDENTIALS;
192 if (!instance->Authenticate)
194 proceed = instance->AuthenticateEx(instance, &settings->Username, &settings->Password,
195 &settings->Domain, reason);
197 return AUTH_CANCELLED;
201 proceed = instance->Authenticate(instance, &settings->Username, &settings->Password,
204 return AUTH_NO_CREDENTIALS;
207 if (utils_str_is_empty(settings->Username) || utils_str_is_empty(settings->Password))
208 return AUTH_NO_CREDENTIALS;
210 if (!utils_sync_credentials(settings, TRUE))
214 if (!utils_str_copy(settings->Username, &origSettings->Username))
216 if (!utils_str_copy(settings->Domain, &origSettings->Domain))
218 if (!utils_str_copy(settings->Password, &origSettings->Password))
220 if (!utils_sync_credentials(origSettings, TRUE))
223 if (!utils_copy_smartcard_settings(settings, origSettings))
229 BOOL utils_sync_credentials(rdpSettings* settings, BOOL toGateway)
231 WINPR_ASSERT(settings);
232 if (!settings->GatewayUseSameCredentials)
237 if (!utils_str_copy(settings->Username, &settings->GatewayUsername))
239 if (!utils_str_copy(settings->Domain, &settings->GatewayDomain))
241 if (!utils_str_copy(settings->Password, &settings->GatewayPassword))
246 if (!utils_str_copy(settings->GatewayUsername, &settings->Username))
248 if (!utils_str_copy(settings->GatewayDomain, &settings->Domain))
250 if (!utils_str_copy(settings->GatewayPassword, &settings->Password))
256 BOOL utils_str_is_empty(
const char* str)
265 BOOL utils_abort_connect(rdpRdp* rdp)
270 return SetEvent(rdp->abortEvent);
273 BOOL utils_reset_abort(rdpRdp* rdp)
277 return ResetEvent(rdp->abortEvent);
280 HANDLE utils_get_abort_event(rdpRdp* rdp)
283 return rdp->abortEvent;
286 BOOL utils_abort_event_is_set(
const rdpRdp* rdp)
290 status = WaitForSingleObject(rdp->abortEvent, 0);
291 return status == WAIT_OBJECT_0;
294 const char* utils_is_vsock(
const char* hostname)
299 const char vsock[8] =
"vsock://";
300 if (strncmp(hostname, vsock,
sizeof(vsock)) == 0)
301 return &hostname[
sizeof(vsock)];
305 static BOOL remove_rdpdr_type(rdpSettings* settings, UINT32 type)
310 printer = freerdp_device_collection_find_type(settings, type);
312 freerdp_device_free(printer);
317 static BOOL disable_clipboard(rdpSettings* settings)
321 freerdp_static_channel_collection_del(settings, CLIPRDR_SVC_CHANNEL_NAME);
325 static BOOL disable_drive(rdpSettings* settings)
332 return remove_rdpdr_type(settings, RDPDR_DTYP_FILESYSTEM);
335 static BOOL disable_printers(rdpSettings* settings)
340 return remove_rdpdr_type(settings, RDPDR_DTYP_PRINT);
343 static BOOL disable_port(rdpSettings* settings)
349 if (!remove_rdpdr_type(settings, RDPDR_DTYP_SERIAL))
351 return remove_rdpdr_type(settings, RDPDR_DTYP_PARALLEL);
354 static BOOL disable_pnp(rdpSettings* settings)
360 static BOOL apply_gw_policy(rdpContext* context)
362 WINPR_ASSERT(context);
363 return utils_reload_channels(context);
366 BOOL utils_apply_gateway_policy(wLog* log, rdpContext* context, UINT32 flags,
const char* module)
369 WINPR_ASSERT(context);
371 rdpSettings* settings = context->settings;
372 WINPR_ASSERT(settings);
374 if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
376 WLog_Print(log, WLOG_DEBUG,
"[%s] policy allows all redirections", module);
380 char buffer[128] = { 0 };
381 WLog_Print(log, WLOG_INFO,
"[%s] policy ignored on user request %s", module,
382 utils_redir_flags_to_string(flags, buffer,
sizeof(buffer)));
384 else if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
386 WLog_Print(log, WLOG_INFO,
"[%s] policy denies all redirections", module);
387 if (!disable_drive(settings))
389 if (!disable_printers(settings))
391 if (!disable_clipboard(settings))
393 if (!disable_port(settings))
395 if (!disable_pnp(settings))
397 if (!apply_gw_policy(context))
402 if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
404 WLog_Print(log, WLOG_INFO,
"[%s] policy denies drive redirections", module);
405 if (!disable_drive(settings))
408 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
410 WLog_Print(log, WLOG_INFO,
"[%s] policy denies printer redirections", module);
411 if (!disable_printers(settings))
414 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
416 WLog_Print(log, WLOG_INFO,
"[%s] policy denies port redirections", module);
417 if (!disable_port(settings))
420 if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
422 WLog_Print(log, WLOG_INFO,
"[%s] policy denies clipboard redirections", module);
423 if (!disable_clipboard(settings))
426 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
428 WLog_Print(log, WLOG_INFO,
"[%s] policy denies PNP redirections", module);
429 if (!disable_pnp(settings))
434 if (!apply_gw_policy(context))
441 char* utils_redir_flags_to_string(UINT32 flags,
char* buffer,
size_t size)
443 winpr_str_append(
"{", buffer, size,
"");
444 if (flags & HTTP_TUNNEL_REDIR_ENABLE_ALL)
445 winpr_str_append(
"ENABLE_ALL", buffer, size,
"|");
446 if (flags & HTTP_TUNNEL_REDIR_DISABLE_ALL)
447 winpr_str_append(
"DISABLE_ALL", buffer, size,
"|");
448 if (flags & HTTP_TUNNEL_REDIR_DISABLE_DRIVE)
449 winpr_str_append(
"DISABLE_DRIVE", buffer, size,
"|");
450 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PRINTER)
451 winpr_str_append(
"DISABLE_PRINTER", buffer, size,
"|");
452 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PORT)
453 winpr_str_append(
"DISABLE_PORT", buffer, size,
"|");
454 if (flags & HTTP_TUNNEL_REDIR_DISABLE_CLIPBOARD)
455 winpr_str_append(
"DISABLE_CLIPBOARD", buffer, size,
"|");
456 if (flags & HTTP_TUNNEL_REDIR_DISABLE_PNP)
457 winpr_str_append(
"DISABLE_PNP", buffer, size,
"|");
459 char fbuffer[16] = { 0 };
460 (void)_snprintf(fbuffer,
sizeof(fbuffer),
"[0x%08" PRIx32
"]", flags);
462 winpr_str_append(fbuffer, buffer, size,
" ");
463 winpr_str_append(
"{", buffer, size,
"}");
467 BOOL utils_reload_channels(rdpContext* context)
469 WINPR_ASSERT(context);
471 freerdp_channels_disconnect(context->channels, context->instance);
472 freerdp_channels_close(context->channels, context->instance);
473 freerdp_channels_free(context->channels);
474 context->channels = freerdp_channels_new(context->instance);
475 WINPR_ASSERT(context->channels);
478 IFCALLRET(context->instance->LoadChannels, rc, context->instance);
480 return freerdp_channels_pre_connect(context->channels, context->instance) == CHANNEL_RC_OK;
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_device_collection_del(rdpSettings *settings, const RDPDR_DEVICE *device)
Removed a device from the settings, returns ownership of the allocated device to caller.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.