17#include <winpr/json.h>
18#include <winpr/path.h>
20#include <freerdp/utils/helpers.h>
21#include <freerdp/crypto/crypto.h>
22#include <freerdp/log.h>
27#define TAG FREERDP_TAG("core.aad-cloud")
29struct freerdp_aad_cloud
35 char* avd_redirect_format;
38static const FreeRDP_AadCloud aad_clouds[] = {
39 {
"commercial",
".wvd.microsoft.com",
"login.microsoftonline.com",
40 "https://www.wvd.microsoft.com/.default",
"https://%s/%s/oauth2/nativeclient" },
41 {
"usgov",
".wvd.azure.us",
"login.microsoftonline.us",
42 "https://www.wvd.azure.us/.default openid profile offline_access",
45 "https://login.microsoftonline.com/common/oauth2/nativeclient" }
48#define AAD_CLOUDS_CONFIG_FILE "aad-clouds.json"
51#define AAD_CLOUD_REDIRECT_CONVERSIONS 2
53static INIT_ONCE aad_clouds_once = INIT_ONCE_STATIC_INIT;
55static FreeRDP_AadCloud* aad_clouds_configured =
nullptr;
56static size_t aad_clouds_configured_count = 0;
57static char** aad_clouds_configured_names =
nullptr;
59static void aad_cloud_entry_free(FreeRDP_AadCloud* cloud)
65 free(cloud->gateway_suffix);
66 free(cloud->authority);
67 free(cloud->avd_scope);
68 free(cloud->avd_redirect_format);
70 const FreeRDP_AadCloud empty = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr };
74static void aad_cloud_table_free(FreeRDP_AadCloud* table,
size_t count)
76 for (
size_t x = 0; x < count; x++)
77 aad_cloud_entry_free(&table[x]);
81WINPR_ATTR_MALLOC(free, 1)
82static
char* printescape(const
char* what)
87 const size_t len = strlen(what);
88 char* peseg = calloc(len + 1, 2);
93 for (
size_t x = 0; x < len; x++)
108static BOOL appendSegment(
const char* seg,
size_t seglen,
char* buffer,
size_t bufferlen)
110 char* eseg = winpr_str_url_encode(seg, seglen);
114 char* peseg = printescape(eseg);
119 const BOOL rc = winpr_str_append(peseg, buffer, bufferlen,
"");
124WINPR_ATTR_MALLOC(free, 1)
125static
char* url_encode(const
char* strWithFormat)
129 const size_t len = strlen(strWithFormat);
130 const char* token = strstr(strWithFormat,
"%s");
133 char* str = printescape(strWithFormat);
136 char* rc = winpr_str_url_encode(str, strlen(str));
142 char* str = calloc(len + 1, 4);
143 const size_t slen = len * 4;
147 const char* last = strWithFormat;
150 const size_t seglen = WINPR_ASSERTING_INT_CAST(
size_t, (token - last));
151 if (!appendSegment(last, seglen, str, slen))
156 winpr_str_append(
"%s", str, slen,
"");
159 token = strstr(last,
"%s");
162 const size_t llen = strnlen(last, len);
165 if (!appendSegment(last, llen, str, slen))
178static BOOL aad_cloud_entry_set(FreeRDP_AadCloud* dst,
const char* name,
const char* suffix,
179 const char* authority,
const char* scope,
const char* redirect)
181 if (!name || !authority || !scope || !redirect)
184 FreeRDP_AadCloud copy = { .name = _strdup(name),
185 .authority = _strdup(authority),
186 .avd_scope = url_encode(scope),
187 .avd_redirect_format = url_encode(redirect),
188 .gateway_suffix = suffix ? _strdup(suffix) : nullptr };
190 if (!copy.name || !copy.authority || !copy.avd_scope || !copy.avd_redirect_format ||
191 (suffix && !copy.gateway_suffix))
193 aad_cloud_entry_free(©);
202static BOOL aad_cloud_table_materialize(FreeRDP_AadCloud** ptable,
size_t* pcount)
204 WINPR_ASSERT(pcount);
206 const size_t count = ARRAYSIZE(aad_clouds);
207 FreeRDP_AadCloud* table = calloc(count,
sizeof(FreeRDP_AadCloud));
211 for (
size_t x = 0; x < count; x++)
213 const FreeRDP_AadCloud* src = &aad_clouds[x];
214 if (!aad_cloud_entry_set(&table[x], src->name, src->gateway_suffix, src->authority,
215 src->avd_scope, src->avd_redirect_format))
217 aad_cloud_table_free(table, count);
228static BOOL aad_cloud_table_merge(FreeRDP_AadCloud** ptable,
size_t* pcount,
229 const FreeRDP_AadCloud* entry)
231 WINPR_ASSERT(ptable);
232 WINPR_ASSERT(pcount);
235 const size_t count = *pcount;
236 for (
size_t x = 0; x < count; x++)
238 FreeRDP_AadCloud* cur = &(*ptable)[x];
239 if (_stricmp(cur->name, entry->name) == 0)
241 WLog_DBG(TAG,
"Overriding duplicate entry %s", entry->name);
242 aad_cloud_entry_free(cur);
248 if (count > SIZE_MAX /
sizeof(FreeRDP_AadCloud) -
sizeof(FreeRDP_AadCloud))
251 FreeRDP_AadCloud* tmp = realloc(*ptable, (count + 1) *
sizeof(FreeRDP_AadCloud));
261static BOOL aad_cloud_valid_gateway_suffix(
const char* value)
263 if (!value || (strlen(value) < 2) || (value[0] !=
'.'))
266 return winpr_str_is_valid_hostname(&value[1]);
271static BOOL aad_cloud_valid_redirect_format(
const char* value)
273 size_t conversions = 0;
274 for (
const char* pos = strstr(value,
"%s"); pos; pos = strstr(pos + 2,
"%s"))
277 return conversions <= AAD_CLOUD_REDIRECT_CONVERSIONS;
281static const char* aad_cloud_json_string(
const WINPR_JSON* obj,
const char* key)
289WINPR_ATTR_MALLOC(free, 1)
290static
char* aad_cloud_json_array_as_list(const WINPR_JSON* obj, const
char* key)
303 if (utils_str_is_empty(str))
305 count += strlen(str) + 1;
308 char* str = calloc(count + 1,
sizeof(
char));
316 if (!winpr_str_append(curstr, str, count,
" "))
326static BOOL aad_cloud_parse_entry(
const char* path,
size_t index, WINPR_JSON* item,
327 FreeRDP_AadCloud* cloud)
331 WLog_WARN(TAG,
"[%s] entry %" PRIuz
" invalid: not a JSON object", path, index);
335 const char* name = aad_cloud_json_string(item,
"name");
336 const char* authority = aad_cloud_json_string(item,
"active_directory");
337 const char* redirect = aad_cloud_json_string(item,
"avd_redirect_format");
338 const char* suffix = aad_cloud_json_string(item,
"gateway_suffix");
340 if (!winpr_str_is_valid_hostname(name))
344 " invalid: 'name' is missing or not a [a-z0-9-] string, got '%s'",
345 path, index, name ? name :
"");
348 if (_stricmp(name, aad_clouds[0].name) == 0)
350 WLog_WARN(TAG,
"[%s] entry %" PRIuz
" invalid: '%s' can not be overridden", path, index,
354 if (!winpr_str_is_valid_hostname(authority))
357 "[%s] entry %" PRIuz
" ('%s') invalid: 'active_directory' is missing or not a "
358 "host name without scheme or path, got '%s'",
359 path, index, name, authority ? authority :
"");
362 if (!aad_cloud_valid_redirect_format(redirect))
365 "[%s] entry %" PRIuz
" ('%s') invalid: 'avd_redirect_format' is missing, empty "
366 "or has more than %d '%%s'",
367 path, index, name, AAD_CLOUD_REDIRECT_CONVERSIONS);
373 "[%s] entry %" PRIuz
" ('%s') invalid: 'gateway_suffix' is not a host name "
374 "starting with the '.' of a label boundary, got '%s'",
375 path, index, name, suffix ? suffix :
"");
382 "[%s] entry %" PRIuz
" ('%s'): 'resource_manager' is accepted and ignored, no "
383 "setting holds an ARM endpoint",
386 char* scope = aad_cloud_json_array_as_list(item,
"avd_scope");
387 if (utils_str_is_empty(scope))
389 WLog_WARN(TAG,
"[%s] entry %" PRIuz
" ('%s') invalid: 'avd_scope' is missing or empty",
394 const BOOL rc = aad_cloud_entry_set(cloud, name, suffix, authority, scope, redirect);
398 WLog_ERR(TAG,
"[%s] entry %" PRIuz
" ('%s') invalid: out of memory", path, index, name);
404static void aad_cloud_load_file(BOOL system, FreeRDP_AadCloud** ptable,
size_t* pcount)
406 char* path = freerdp_GetConfigFilePath(system, AAD_CLOUDS_CONFIG_FILE);
410 WINPR_JSON* json = freerdp_GetJSONConfigFile(system, AAD_CLOUDS_CONFIG_FILE);
413 if (winpr_PathFileExists(path))
414 WLog_WARN(TAG,
"[%s] ignored: the file does not parse as JSON", path);
416 WLog_DBG(TAG,
"[%s] no AVD cloud configuration file", path);
422 WLog_WARN(TAG,
"[%s] ignored: expected a JSON array of cloud objects", path);
427 for (
size_t x = 0; x < count; x++)
429 FreeRDP_AadCloud entry = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr };
433 WLog_DBG(TAG,
"[%s] configured AVD cloud '%s'", path, entry.name);
434 if (!aad_cloud_table_merge(ptable, pcount, &entry))
436 WLog_ERR(TAG,
"[%s] entry %" PRIuz
" ignored: out of memory", path, x);
437 aad_cloud_entry_free(&entry);
447static BOOL CALLBACK aad_clouds_init(WINPR_ATTR_UNUSED PINIT_ONCE once,
448 WINPR_ATTR_UNUSED PVOID param,
449 WINPR_ATTR_UNUSED PVOID* context)
451 if (!aad_cloud_table_materialize(&aad_clouds_configured, &aad_clouds_configured_count))
453 aad_cloud_load_file(TRUE, &aad_clouds_configured, &aad_clouds_configured_count);
454 aad_cloud_load_file(FALSE, &aad_clouds_configured, &aad_clouds_configured_count);
456 if (aad_clouds_configured_count == 0)
459 aad_clouds_configured_names = (
char**)calloc(aad_clouds_configured_count,
sizeof(
const char*));
460 if (!aad_clouds_configured_names)
462 for (
size_t x = 0; x < aad_clouds_configured_count; x++)
464 aad_clouds_configured_names[x] = aad_clouds_configured[x].name;
470static const FreeRDP_AadCloud* aad_cloud_table(
size_t* pcount)
472 WINPR_ASSERT(pcount);
474 if (InitOnceExecuteOnce(&aad_clouds_once, aad_clouds_init,
nullptr,
nullptr) &&
475 aad_clouds_configured)
477 *pcount = aad_clouds_configured_count;
478 return aad_clouds_configured;
481 *pcount = ARRAYSIZE(aad_clouds);
485const FreeRDP_AadCloud* freerdp_utils_aad_cloud_by_name(
const char* name)
491 const FreeRDP_AadCloud* clouds = aad_cloud_table(&count);
492 for (
size_t x = 0; x < count; x++)
494 if (_stricmp(name, clouds[x].name) == 0)
500const FreeRDP_AadCloud* freerdp_utils_aad_cloud_for_gateway(
const char* hostname)
506 const FreeRDP_AadCloud* clouds = aad_cloud_table(&count);
507 size_t hostlen = strlen(hostname);
508 if ((hostlen > 0) && (hostname[hostlen - 1] ==
'.'))
510 for (
size_t x = 0; x < count; x++)
512 const char* suffix = clouds[x].gateway_suffix;
516 const size_t suffixlen = strlen(suffix);
517 if ((hostlen >= suffixlen) &&
518 (_strnicmp(&hostname[hostlen - suffixlen], suffix, suffixlen) == 0))
524BOOL freerdp_utils_aad_apply_cloud(rdpSettings* settings,
const FreeRDP_AadCloud* cloud)
526 if (!settings || !cloud)
529 const char* authority = cloud->authority;
530 const char* scope = cloud->avd_scope;
531 const char* redirect = cloud->avd_redirect_format;
532 if (!authority || !scope || !redirect)
548 WINPR_ASSERT(pcount);
550 if (!InitOnceExecuteOnce(&aad_clouds_once, aad_clouds_init,
nullptr,
nullptr))
553 *pcount = aad_clouds_configured_count;
559 cnv.ppc = aad_clouds_configured_names;
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
WINPR_ATTR_NODISCARD WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
const char ** freerdp_utils_aad_configuration_list(size_t *pcount)
return a list of configurations supported by build
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.