static BOOL nla_adjust_settings_from_smartcard(rdpNla* nla)
{
	SmartcardCerts* certs = NULL;
	const SmartcardCertInfo* info = NULL;
	DWORD count;
	rdpSettings* settings;
	SEC_WINPR_KERBEROS_SETTINGS* kerbSettings;
	BOOL ret = FALSE;

	WINPR_ASSERT(nla);
	WINPR_ASSERT(nla->rdpcontext);

	settings = nla->rdpcontext->settings;
	WINPR_ASSERT(settings);

	if (!settings->SmartcardLogon)
		return TRUE;

	kerbSettings = nla->kerberosSettings;
	WINPR_ASSERT(kerbSettings);

	if (!settings->CspName)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_CspName, MS_SCARD_PROV_A))
		{
			WLog_ERR(TAG, "unable to set CSP name");
			return FALSE;
		}
	}

	if (settings->PkinitAnchors)
	{
		kerbSettings->pkinitX509Anchors = _strdup(settings->PkinitAnchors);
		if (!kerbSettings->pkinitX509Anchors)
		{
			WLog_ERR(TAG, "error setting X509 anchors");
			return FALSE;
		}
	}

	if (!smartcard_enumerateCerts(settings, &certs, &count))
	{
		WLog_ERR(TAG, "unable to list smartcard certificates");
		return FALSE;
	}

	if (count < 1)
	{
		WLog_ERR(TAG, "no smartcard certificates found");
		goto out;
	}

	if (count != 1)
		goto setup_pin;

	info = smartcard_getCertInfo(certs, 0);
	if (!info)
		goto out;

	/*
	 * just one result let's try to fill missing parameters
	 */
	if (!settings->Username && info->userHint)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_Username, info->userHint))
		{
			WLog_ERR(TAG, "unable to copy certificate username");
			goto out;
		}
	}

	if (!settings->Domain && info->domainHint)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_Domain, info->domainHint))
		{
			WLog_ERR(TAG, "unable to copy certificate domain");
			goto out;
		}
	}

	if (!settings->ReaderName && info->reader)
	{
		if (ConvertFromUnicode(CP_UTF8, 0, info->reader, -1, &settings->ReaderName, 0, NULL, NULL) <
		    0)
		{
			WLog_ERR(TAG, "unable to copy reader name");
			goto out;
		}
	}

	if (!settings->ContainerName && info->containerName)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_ContainerName, info->containerName))
		{
			WLog_ERR(TAG, "unable to copy container name");
			goto out;
		}
	}

	memcpy(kerbSettings->certSha1, info->sha1Hash, sizeof(kerbSettings->certSha1));

	if (info->pkinitArgs)
	{
		kerbSettings->pkinitX509Identity = _strdup(info->pkinitArgs);
		if (!kerbSettings->pkinitX509Identity)
		{
			WLog_ERR(TAG, "unable to copy pkinitArgs");
			goto out;
		}
	}

setup_pin:

	ret = TRUE;
out:
	smartcardCerts_Free(certs);

	return ret;
}
static BOOL nla_adjust_settings_from_smartcard(rdpNla* nla)
{
	SmartcardCerts* certs = NULL;
	const SmartcardCertInfo* info = NULL;
	DWORD count;
	rdpSettings* settings;
	SEC_WINPR_KERBEROS_SETTINGS* kerbSettings;
	BOOL ret = FALSE;

	WINPR_ASSERT(nla);
	WINPR_ASSERT(nla->rdpcontext);

	settings = nla->rdpcontext->settings;
	WINPR_ASSERT(settings);

	if (!settings->SmartcardLogon)
		return TRUE;

	kerbSettings = nla->kerberosSettings;
	WINPR_ASSERT(kerbSettings);

	if (!settings->CspName)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_CspName, MS_SCARD_PROV_A))
		{
			WLog_ERR(TAG, "unable to set CSP name");
			return FALSE;
		}
	}

	if (settings->PkinitAnchors)
	{
		kerbSettings->pkinitX509Anchors = _strdup(settings->PkinitAnchors);
		if (!kerbSettings->pkinitX509Anchors)
		{
			WLog_ERR(TAG, "error setting X509 anchors");
			return FALSE;
		}
	}

	if (!smartcard_enumerateCerts(settings, &certs, &count))
	{
		WLog_ERR(TAG, "unable to list smartcard certificates");
		return FALSE;
	}

	if (count < 1)
	{
		WLog_ERR(TAG, "no smartcard certificates found");
		goto out;
	}

	if (count != 1)
		goto setup_pin;

	info = smartcard_getCertInfo(certs, 0);
	if (!info)
		goto out;

	 
	if (!settings->Username && info->userHint)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_Username, info->userHint))
		{
			WLog_ERR(TAG, "unable to copy certificate username");
			goto out;
		}
	}

	if (!settings->Domain && info->domainHint)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_Domain, info->domainHint))
		{
			WLog_ERR(TAG, "unable to copy certificate domain");
			goto out;
		}
	}

	if (!settings->ReaderName && info->reader)
	{
		if (ConvertFromUnicode(CP_UTF8, 0, info->reader, -1, &settings->ReaderName, 0, NULL, NULL) <
		    0)
		{
			WLog_ERR(TAG, "unable to copy reader name");
			goto out;
		}
	}

	if (!settings->ContainerName && info->containerName)
	{
		if (!freerdp_settings_set_string(settings, FreeRDP_ContainerName, info->containerName))
		{
			WLog_ERR(TAG, "unable to copy container name");
			goto out;
		}
	}

	memcpy(kerbSettings->certSha1, info->sha1Hash, sizeof(kerbSettings->certSha1));

	if (info->pkinitArgs)
	{
		kerbSettings->pkinitX509Identity = _strdup(info->pkinitArgs);
		if (!kerbSettings->pkinitX509Identity)
		{
			WLog_ERR(TAG, "unable to copy pkinitArgs");
			goto out;
		}
	}

setup_pin:

	ret = TRUE;
out:
	smartcardCerts_Free(certs);

	return ret;
}