FreeRDP
Loading...
Searching...
No Matches
aad_cloud.c
1
17#include <winpr/json.h>
18#include <winpr/path.h>
19
20#include <freerdp/utils/helpers.h>
21#include <freerdp/crypto/crypto.h>
22#include <freerdp/log.h>
23
24#include "utils.h"
25#include "aad_cloud.h"
26
27#define TAG FREERDP_TAG("core.aad-cloud")
28
29struct freerdp_aad_cloud
30{
31 char* name;
32 char* gateway_suffix;
33 char* authority;
34 char* avd_scope;
35 char* avd_redirect_format;
36};
37
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",
43 /* the registered redirect of the AVD public client (#11032), the .us form is rejected
44 * with AADSTS50011 */
45 "https://login.microsoftonline.com/common/oauth2/nativeclient" }
46};
47
48#define AAD_CLOUDS_CONFIG_FILE "aad-clouds.json"
49
50/* '%s' conversions of the redirect format: authority and tenant id */
51#define AAD_CLOUD_REDIRECT_CONVERSIONS 2
52
53static INIT_ONCE aad_clouds_once = INIT_ONCE_STATIC_INIT;
54
55static FreeRDP_AadCloud* aad_clouds_configured = nullptr;
56static size_t aad_clouds_configured_count = 0;
57static char** aad_clouds_configured_names = nullptr;
58
59static void aad_cloud_entry_free(FreeRDP_AadCloud* cloud)
60{
61 if (!cloud)
62 return;
63
64 free(cloud->name);
65 free(cloud->gateway_suffix);
66 free(cloud->authority);
67 free(cloud->avd_scope);
68 free(cloud->avd_redirect_format);
69
70 const FreeRDP_AadCloud empty = { nullptr, nullptr, nullptr, nullptr, nullptr };
71 *cloud = empty;
72}
73
74static void aad_cloud_table_free(FreeRDP_AadCloud* table, size_t count)
75{
76 for (size_t x = 0; x < count; x++)
77 aad_cloud_entry_free(&table[x]);
78 free(table);
79}
80
81WINPR_ATTR_MALLOC(free, 1)
82static char* printescape(const char* what)
83{
84 if (!what)
85 return nullptr;
86
87 const size_t len = strlen(what);
88 char* peseg = calloc(len + 1, 2);
89 if (!peseg)
90 return nullptr;
91
92 size_t pos = 0;
93 for (size_t x = 0; x < len; x++)
94 {
95 char cur = what[x];
96 if (cur == '%')
97 {
98 peseg[pos++] = '%';
99 peseg[pos++] = '%';
100 }
101 else
102 peseg[pos++] = cur;
103 }
104 return peseg;
105}
106
107WINPR_ATTR_NODISCARD
108static BOOL appendSegment(const char* seg, size_t seglen, char* buffer, size_t bufferlen)
109{
110 char* eseg = winpr_str_url_encode(seg, seglen);
111 if (!eseg)
112 return FALSE;
113
114 char* peseg = printescape(eseg);
115 free(eseg);
116 if (!peseg)
117 return FALSE;
118
119 const BOOL rc = winpr_str_append(peseg, buffer, bufferlen, "");
120 free(peseg);
121 return rc;
122}
123
124WINPR_ATTR_MALLOC(free, 1)
125static char* url_encode(const char* strWithFormat)
126{
127 if (!strWithFormat)
128 return nullptr;
129 const size_t len = strlen(strWithFormat);
130 const char* token = strstr(strWithFormat, "%s");
131 if (!token)
132 {
133 char* str = printescape(strWithFormat);
134 if (!str)
135 return nullptr;
136 char* rc = winpr_str_url_encode(str, strlen(str));
137 free(str);
138 return rc;
139 }
140
141 // Overallocate. URL encoding will use at most 4 times the original length.
142 char* str = calloc(len + 1, 4);
143 const size_t slen = len * 4;
144 if (!str)
145 return nullptr;
146
147 const char* last = strWithFormat;
148 do
149 {
150 const size_t seglen = WINPR_ASSERTING_INT_CAST(size_t, (token - last));
151 if (!appendSegment(last, seglen, str, slen))
152 {
153 free(str);
154 return nullptr;
155 }
156 winpr_str_append("%s", str, slen, "");
157
158 last = token + 2;
159 token = strstr(last, "%s");
160 if (!token)
161 {
162 const size_t llen = strnlen(last, len);
163 if (llen > 0)
164 {
165 if (!appendSegment(last, llen, str, slen))
166 {
167 free(str);
168 return nullptr;
169 }
170 }
171 }
172 } while (token);
173
174 return str;
175}
176
177WINPR_ATTR_NODISCARD
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)
180{
181 if (!name || !authority || !scope || !redirect)
182 return FALSE;
183
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 };
189
190 if (!copy.name || !copy.authority || !copy.avd_scope || !copy.avd_redirect_format ||
191 (suffix && !copy.gateway_suffix))
192 {
193 aad_cloud_entry_free(&copy);
194 return FALSE;
195 }
196
197 *dst = copy;
198 return TRUE;
199}
200
201WINPR_ATTR_NODISCARD
202static BOOL aad_cloud_table_materialize(FreeRDP_AadCloud** ptable, size_t* pcount)
203{
204 WINPR_ASSERT(pcount);
205
206 const size_t count = ARRAYSIZE(aad_clouds);
207 FreeRDP_AadCloud* table = calloc(count, sizeof(FreeRDP_AadCloud));
208 if (!table)
209 return FALSE;
210
211 for (size_t x = 0; x < count; x++)
212 {
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))
216 {
217 aad_cloud_table_free(table, count);
218 return FALSE;
219 }
220 }
221
222 *ptable = table;
223 *pcount = count;
224 return TRUE;
225}
226
227WINPR_ATTR_NODISCARD
228static BOOL aad_cloud_table_merge(FreeRDP_AadCloud** ptable, size_t* pcount,
229 const FreeRDP_AadCloud* entry)
230{
231 WINPR_ASSERT(ptable);
232 WINPR_ASSERT(pcount);
233 WINPR_ASSERT(entry);
234
235 const size_t count = *pcount;
236 for (size_t x = 0; x < count; x++)
237 {
238 FreeRDP_AadCloud* cur = &(*ptable)[x];
239 if (_stricmp(cur->name, entry->name) == 0)
240 {
241 WLog_DBG(TAG, "Overriding duplicate entry %s", entry->name);
242 aad_cloud_entry_free(cur);
243 *cur = *entry;
244 return TRUE;
245 }
246 }
247
248 if (count > SIZE_MAX / sizeof(FreeRDP_AadCloud) - sizeof(FreeRDP_AadCloud))
249 return FALSE;
250
251 FreeRDP_AadCloud* tmp = realloc(*ptable, (count + 1) * sizeof(FreeRDP_AadCloud));
252 if (!tmp)
253 return FALSE;
254 tmp[count] = *entry;
255 *ptable = tmp;
256 *pcount = count + 1;
257 return TRUE;
258}
259
260WINPR_ATTR_NODISCARD
261static BOOL aad_cloud_valid_gateway_suffix(const char* value)
262{
263 if (!value || (strlen(value) < 2) || (value[0] != '.'))
264 return FALSE;
265
266 return winpr_str_is_valid_hostname(&value[1]);
267}
268
269/* plain URL with at most two '%s' for the authority and the tenant id */
270WINPR_ATTR_NODISCARD
271static BOOL aad_cloud_valid_redirect_format(const char* value)
272{
273 size_t conversions = 0;
274 for (const char* pos = strstr(value, "%s"); pos; pos = strstr(pos + 2, "%s"))
275 conversions++;
276
277 return conversions <= AAD_CLOUD_REDIRECT_CONVERSIONS;
278}
279
280WINPR_ATTR_NODISCARD
281static const char* aad_cloud_json_string(const WINPR_JSON* obj, const char* key)
282{
283 WINPR_JSON* item = WINPR_JSON_GetObjectItemCaseSensitive(obj, key);
284 if (!item || !WINPR_JSON_IsString(item))
285 return nullptr;
286 return WINPR_JSON_GetStringValue(item);
287}
288
289WINPR_ATTR_MALLOC(free, 1)
290static char* aad_cloud_json_array_as_list(const WINPR_JSON* obj, const char* key)
291{
292 WINPR_JSON* item = WINPR_JSON_GetObjectItemCaseSensitive(obj, key);
293 if (!item || !WINPR_JSON_IsArray(item))
294 return nullptr;
295
296 size_t count = 0;
297 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
298 {
299 WINPR_JSON* cur = WINPR_JSON_GetArrayItem(item, x);
300 if (!cur || !WINPR_JSON_IsString(cur))
301 return nullptr;
302 const char* str = WINPR_JSON_GetStringValue(cur);
303 if (utils_str_is_empty(str))
304 return nullptr;
305 count += strlen(str) + 1;
306 }
307
308 char* str = calloc(count + 1, sizeof(char));
309 if (!str)
310 return nullptr;
311
312 for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
313 {
314 WINPR_JSON* cur = WINPR_JSON_GetArrayItem(item, x);
315 const char* curstr = WINPR_JSON_GetStringValue(cur);
316 if (!winpr_str_append(curstr, str, count, " "))
317 {
318 free(str);
319 return nullptr;
320 }
321 }
322 return str;
323}
324
325WINPR_ATTR_NODISCARD
326static BOOL aad_cloud_parse_entry(const char* path, size_t index, WINPR_JSON* item,
327 FreeRDP_AadCloud* cloud)
328{
329 if (!WINPR_JSON_IsObject(item))
330 {
331 WLog_WARN(TAG, "[%s] entry %" PRIuz " invalid: not a JSON object", path, index);
332 return FALSE;
333 }
334
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");
339
340 if (!winpr_str_is_valid_hostname(name))
341 {
342 WLog_WARN(TAG,
343 "[%s] entry %" PRIuz
344 " invalid: 'name' is missing or not a [a-z0-9-] string, got '%s'",
345 path, index, name ? name : "");
346 return FALSE;
347 }
348 if (_stricmp(name, aad_clouds[0].name) == 0)
349 {
350 WLog_WARN(TAG, "[%s] entry %" PRIuz " invalid: '%s' can not be overridden", path, index,
351 name);
352 return FALSE;
353 }
354 if (!winpr_str_is_valid_hostname(authority))
355 {
356 WLog_WARN(TAG,
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 : "");
360 return FALSE;
361 }
362 if (!aad_cloud_valid_redirect_format(redirect))
363 {
364 WLog_WARN(TAG,
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);
368 return FALSE;
369 }
370 if (WINPR_JSON_HasObjectItem(item, "gateway_suffix") && !aad_cloud_valid_gateway_suffix(suffix))
371 {
372 WLog_WARN(TAG,
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 : "");
376 return FALSE;
377 }
378
379 if (WINPR_JSON_HasObjectItem(item, "resource_manager"))
380 {
381 WLog_DBG(TAG,
382 "[%s] entry %" PRIuz " ('%s'): 'resource_manager' is accepted and ignored, no "
383 "setting holds an ARM endpoint",
384 path, index, name);
385 }
386 char* scope = aad_cloud_json_array_as_list(item, "avd_scope");
387 if (utils_str_is_empty(scope))
388 {
389 WLog_WARN(TAG, "[%s] entry %" PRIuz " ('%s') invalid: 'avd_scope' is missing or empty",
390 path, index, name);
391 free(scope);
392 return FALSE;
393 }
394 const BOOL rc = aad_cloud_entry_set(cloud, name, suffix, authority, scope, redirect);
395 free(scope);
396 if (!rc)
397 {
398 WLog_ERR(TAG, "[%s] entry %" PRIuz " ('%s') invalid: out of memory", path, index, name);
399 return FALSE;
400 }
401 return TRUE;
402}
403
404static void aad_cloud_load_file(BOOL system, FreeRDP_AadCloud** ptable, size_t* pcount)
405{
406 char* path = freerdp_GetConfigFilePath(system, AAD_CLOUDS_CONFIG_FILE);
407 if (!path)
408 return;
409
410 WINPR_JSON* json = freerdp_GetJSONConfigFile(system, AAD_CLOUDS_CONFIG_FILE);
411 if (!json)
412 {
413 if (winpr_PathFileExists(path))
414 WLog_WARN(TAG, "[%s] ignored: the file does not parse as JSON", path);
415 else
416 WLog_DBG(TAG, "[%s] no AVD cloud configuration file", path);
417 goto fail;
418 }
419
420 if (!WINPR_JSON_IsArray(json))
421 {
422 WLog_WARN(TAG, "[%s] ignored: expected a JSON array of cloud objects", path);
423 goto fail;
424 }
425
426 const size_t count = WINPR_JSON_GetArraySize(json);
427 for (size_t x = 0; x < count; x++)
428 {
429 FreeRDP_AadCloud entry = { nullptr, nullptr, nullptr, nullptr, nullptr };
430 if (!aad_cloud_parse_entry(path, x, WINPR_JSON_GetArrayItem(json, x), &entry))
431 goto fail;
432
433 WLog_DBG(TAG, "[%s] configured AVD cloud '%s'", path, entry.name);
434 if (!aad_cloud_table_merge(ptable, pcount, &entry))
435 {
436 WLog_ERR(TAG, "[%s] entry %" PRIuz " ignored: out of memory", path, x);
437 aad_cloud_entry_free(&entry);
438 }
439 }
440
441fail:
442 WINPR_JSON_Delete(json);
443 free(path);
444}
445
446WINPR_ATTR_NODISCARD
447static BOOL CALLBACK aad_clouds_init(WINPR_ATTR_UNUSED PINIT_ONCE once,
448 WINPR_ATTR_UNUSED PVOID param,
449 WINPR_ATTR_UNUSED PVOID* context)
450{
451 if (!aad_cloud_table_materialize(&aad_clouds_configured, &aad_clouds_configured_count))
452 return FALSE;
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);
455
456 if (aad_clouds_configured_count == 0)
457 return TRUE;
458
459 aad_clouds_configured_names = (char**)calloc(aad_clouds_configured_count, sizeof(const char*));
460 if (!aad_clouds_configured_names)
461 return FALSE;
462 for (size_t x = 0; x < aad_clouds_configured_count; x++)
463 {
464 aad_clouds_configured_names[x] = aad_clouds_configured[x].name;
465 }
466 return TRUE;
467}
468
469WINPR_ATTR_NODISCARD
470static const FreeRDP_AadCloud* aad_cloud_table(size_t* pcount)
471{
472 WINPR_ASSERT(pcount);
473
474 if (InitOnceExecuteOnce(&aad_clouds_once, aad_clouds_init, nullptr, nullptr) &&
475 aad_clouds_configured)
476 {
477 *pcount = aad_clouds_configured_count;
478 return aad_clouds_configured;
479 }
480
481 *pcount = ARRAYSIZE(aad_clouds);
482 return aad_clouds;
483}
484
485const FreeRDP_AadCloud* freerdp_utils_aad_cloud_by_name(const char* name)
486{
487 if (!name)
488 return nullptr;
489
490 size_t count = 0;
491 const FreeRDP_AadCloud* clouds = aad_cloud_table(&count);
492 for (size_t x = 0; x < count; x++)
493 {
494 if (_stricmp(name, clouds[x].name) == 0)
495 return &clouds[x];
496 }
497 return nullptr;
498}
499
500const FreeRDP_AadCloud* freerdp_utils_aad_cloud_for_gateway(const char* hostname)
501{
502 if (!hostname)
503 return nullptr;
504
505 size_t count = 0;
506 const FreeRDP_AadCloud* clouds = aad_cloud_table(&count);
507 size_t hostlen = strlen(hostname);
508 if ((hostlen > 0) && (hostname[hostlen - 1] == '.'))
509 hostlen--;
510 for (size_t x = 0; x < count; x++)
511 {
512 const char* suffix = clouds[x].gateway_suffix;
513 if (!suffix)
514 continue;
515
516 const size_t suffixlen = strlen(suffix);
517 if ((hostlen >= suffixlen) &&
518 (_strnicmp(&hostname[hostlen - suffixlen], suffix, suffixlen) == 0))
519 return &clouds[x];
520 }
521 return nullptr;
522}
523
524BOOL freerdp_utils_aad_apply_cloud(rdpSettings* settings, const FreeRDP_AadCloud* cloud)
525{
526 if (!settings || !cloud)
527 return FALSE;
528
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)
533 return FALSE;
534
535 BOOL rc = freerdp_settings_set_string(settings, FreeRDP_GatewayAzureActiveDirectory, authority);
536 if (!rc)
537 return FALSE;
538
539 rc = freerdp_settings_set_string(settings, FreeRDP_GatewayAvdScope, scope);
540 if (!rc)
541 return FALSE;
542
543 return freerdp_settings_set_string(settings, FreeRDP_GatewayAvdAccessAadFormat, redirect);
544}
545
546const char** freerdp_utils_aad_configuration_list(size_t* pcount)
547{
548 WINPR_ASSERT(pcount);
549 *pcount = 0;
550 if (!InitOnceExecuteOnce(&aad_clouds_once, aad_clouds_init, nullptr, nullptr))
551 return nullptr;
552
553 *pcount = aad_clouds_configured_count;
554 union
555 {
556 char** ppc;
557 const char** cppc;
558 } cnv;
559 cnv.ppc = aad_clouds_configured_names;
560 return cnv.cppc;
561}
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.
Definition c-json.c:132
WINPR_ATTR_NODISCARD WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
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.
Definition c-json.c:127
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:194
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.
Definition c-json.c:108
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:199
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition c-json.c:204
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142
const char ** freerdp_utils_aad_configuration_list(size_t *pcount)
return a list of configurations supported by build
Definition aad_cloud.c:546
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.