Skip to content

Commit d2395d2

Browse files
authored
Merge pull request FreeRDP#11634 from akallabeth/tls-config
JSON configuration helpers
2 parents 2ebc5cd + 4f4c3e6 commit d2395d2

8 files changed

Lines changed: 91 additions & 151 deletions

File tree

client/SDL/common/sdl_prefs.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ namespace fs = std::experimental::filesystem;
4141
SdlPref::WINPR_JSONPtr SdlPref::get()
4242
{
4343
auto config = get_pref_file();
44-
45-
std::ifstream ifs(config);
46-
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
47-
return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
44+
return { WINPR_JSON_ParseFromFile(config.c_str()), WINPR_JSON_Delete };
4845
}
4946

5047
WINPR_JSON* SdlPref::get_item(const std::string& key)

include/freerdp/utils/helpers.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#pragma once
2222

2323
#include <winpr/wtypes.h>
24+
#include <winpr/json.h>
25+
2426
#include <freerdp/api.h>
2527

2628
/** @brief Return the absolute path of a configuration file (the path of the configuration
@@ -38,3 +40,15 @@
3840
*/
3941
WINPR_ATTR_MALLOC(free, 1)
4042
FREERDP_API char* freerdp_GetConfigFilePath(BOOL system, const char* filename);
43+
44+
/** @brief return a parsed JSON for a given config file name.
45+
*
46+
* @param system a boolean indicating the configuration base, \b TRUE for system configuration,
47+
* \b FALSE for user configuration
48+
* @param filename an optional configuration file name to append.
49+
*
50+
* @return A parsed \b WINPR_JSON object or \b NULL in case of any failure.
51+
* @since version 3.16.0
52+
*/
53+
WINPR_ATTR_MALLOC(WINPR_JSON_Delete, 1)
54+
FREERDP_API WINPR_JSON* freerdp_GetJSONConfigFile(BOOL system, const char* filename);

libfreerdp/crypto/tls.c

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,42 +1498,6 @@ static int tls_config_parse_bool(WINPR_JSON* json, const char* opt)
14981498
return 0;
14991499
}
15001500

1501-
static char* tls_config_read(const char* configfile)
1502-
{
1503-
char* data = NULL;
1504-
FILE* fp = winpr_fopen(configfile, "r");
1505-
if (!fp)
1506-
return NULL;
1507-
1508-
const int rc = fseek(fp, 0, SEEK_END);
1509-
if (rc != 0)
1510-
goto fail;
1511-
1512-
const INT64 size = _ftelli64(fp);
1513-
if (size <= 0)
1514-
goto fail;
1515-
1516-
const int rc2 = fseek(fp, 0, SEEK_SET);
1517-
if (rc2 != 0)
1518-
goto fail;
1519-
1520-
data = calloc((size_t)size + 1, sizeof(char));
1521-
if (!data)
1522-
goto fail;
1523-
1524-
const size_t read = fread(data, 1, (size_t)size, fp);
1525-
if (read != (size_t)size)
1526-
{
1527-
free(data);
1528-
data = NULL;
1529-
goto fail;
1530-
}
1531-
1532-
fail:
1533-
fclose(fp);
1534-
return data;
1535-
}
1536-
15371501
static int tls_config_check_allowed_hashed(const char* configfile, const rdpCertificate* cert,
15381502
WINPR_JSON* json)
15391503
{
@@ -1604,26 +1568,12 @@ static int tls_config_check_certificate(const rdpCertificate* cert, BOOL* pAllow
16041568
WINPR_ASSERT(pAllowUserconfig);
16051569

16061570
int rc = 0;
1607-
char* configfile = freerdp_GetConfigFilePath(TRUE, "certificates.json");
1608-
WINPR_JSON* json = NULL;
1609-
1610-
if (!configfile)
1611-
{
1612-
WLog_DBG(TAG, "No configuration file for certificate handling, asking user");
1613-
goto fail;
1614-
}
1571+
const char configfile[] = "certificates.json";
1572+
WINPR_JSON* json = freerdp_GetJSONConfigFile(TRUE, configfile);
16151573

1616-
char* configdata = tls_config_read(configfile);
1617-
if (!configdata)
1618-
{
1619-
WLog_DBG(TAG, "Configuration file for certificate handling, asking user");
1620-
goto fail;
1621-
}
1622-
json = WINPR_JSON_Parse(configdata);
16231574
if (!json)
16241575
{
1625-
WLog_DBG(TAG, "No valid configuration file '%s' for certificate handling, asking user",
1626-
configfile);
1576+
WLog_DBG(TAG, "No or no valid configuration file for certificate handling, asking user");
16271577
goto fail;
16281578
}
16291579

@@ -1659,7 +1609,6 @@ static int tls_config_check_certificate(const rdpCertificate* cert, BOOL* pAllow
16591609

16601610
*pAllowUserconfig = (rc == 0);
16611611
WINPR_JSON_Delete(json);
1662-
free(configfile);
16631612
return rc;
16641613
}
16651614

libfreerdp/locale/keyboard_layout.c

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ static BOOL load_layout_file(void)
924924
if (!fp)
925925
goto end;
926926
(void)fprintf(fp, "%s", str);
927-
fclose(fp);
927+
(void)fclose(fp);
928928
}
929929
end:
930930
free(str);
@@ -1006,54 +1006,9 @@ static void clear_layout_tables(void)
10061006

10071007
static WINPR_JSON* load_layouts_from_file(const char* filename)
10081008
{
1009-
INT64 jstrlen = 0;
1010-
char* jstr = NULL;
1011-
WINPR_JSON* json = NULL;
1012-
FILE* fp = winpr_fopen(filename, "r");
1013-
if (!fp)
1014-
{
1015-
WLog_WARN(TAG, "resource file '%s' does not exist or is not readable", filename);
1016-
return NULL;
1017-
}
1018-
1019-
if (_fseeki64(fp, 0, SEEK_END) < 0)
1020-
{
1021-
WLog_WARN(TAG, "resource file '%s' seek failed", filename);
1022-
goto end;
1023-
}
1024-
jstrlen = _ftelli64(fp);
1025-
if (jstrlen < 0)
1026-
{
1027-
WLog_WARN(TAG, "resource file '%s' invalid length %" PRId64, filename, jstrlen);
1028-
goto end;
1029-
}
1030-
if (_fseeki64(fp, 0, SEEK_SET) < 0)
1031-
{
1032-
WLog_WARN(TAG, "resource file '%s' seek failed", filename);
1033-
goto end;
1034-
}
1035-
1036-
jstr = calloc((size_t)jstrlen + 1, sizeof(char));
1037-
if (!jstr)
1038-
{
1039-
WLog_WARN(TAG, "resource file '%s' failed to allocate buffer of size %" PRId64, filename,
1040-
jstrlen);
1041-
goto end;
1042-
}
1043-
1044-
if (fread(jstr, (size_t)jstrlen, sizeof(char), fp) != 1)
1045-
{
1046-
WLog_WARN(TAG, "resource file '%s' failed to read buffer of size %" PRId64, filename,
1047-
jstrlen);
1048-
goto end;
1049-
}
1050-
1051-
json = WINPR_JSON_ParseWithLength(jstr, (size_t)jstrlen);
1009+
WINPR_JSON* json = WINPR_JSON_ParseFromFile(filename);
10521010
if (!json)
10531011
WLog_WARN(TAG, "resource file '%s' is not a valid JSON file", filename);
1054-
end:
1055-
fclose(fp);
1056-
free(jstr);
10571012
return json;
10581013
}
10591014

libfreerdp/utils/helpers.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,14 @@ char* freerdp_GetConfigFilePath(BOOL system, const char* filename)
5959
free(base);
6060
return path;
6161
}
62+
63+
WINPR_JSON* freerdp_GetJSONConfigFile(BOOL system, const char* filename)
64+
{
65+
char* path = freerdp_GetConfigFilePath(system, filename);
66+
if (!path)
67+
return NULL;
68+
69+
WINPR_JSON* json = WINPR_JSON_ParseFromFile(path);
70+
free(path);
71+
return json;
72+
}

winpr/include/winpr/json.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ extern "C"
7777
WINPR_ATTR_MALLOC(WINPR_JSON_Delete, 1)
7878
WINPR_API WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length);
7979

80+
/**
81+
* @brief Parse a JSON string read from a file \b filename
82+
*
83+
* @param filename the name of the file to read from
84+
* @return A @ref WINPR_JSON object holding the parsed string or \b NULL if failed
85+
* @since version 3.16.0
86+
*/
87+
WINPR_ATTR_MALLOC(WINPR_JSON_Delete, 1)
88+
WINPR_API WINPR_JSON* WINPR_JSON_ParseFromFile(const char* filename);
89+
90+
/**
91+
* @brief Parse a JSON string read from a \b FILE
92+
*
93+
* @param fp a \b FILE pointer to read from.
94+
* @return A @ref WINPR_JSON object holding the parsed string or \b NULL if failed
95+
* @since version 3.16.0
96+
*/
97+
WINPR_ATTR_MALLOC(WINPR_JSON_Delete, 1)
98+
WINPR_API WINPR_JSON* WINPR_JSON_ParseFromFileFP(FILE* fp);
99+
80100
/**
81101
* @brief Get the number of arrayitems from an array
82102
*

winpr/libwinpr/timezone/TimeZoneNameMapUtils.c

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -149,54 +149,9 @@ static BOOL tz_parse_json_entry(WINPR_JSON* json, size_t pos, TimeZoneNameMapEnt
149149

150150
static WINPR_JSON* load_timezones_from_file(const char* filename)
151151
{
152-
INT64 jstrlen = 0;
153-
char* jstr = NULL;
154-
WINPR_JSON* json = NULL;
155-
FILE* fp = winpr_fopen(filename, "r");
156-
if (!fp)
157-
{
158-
WLog_WARN(TAG, "Timezone resource file '%s' does not exist or is not readable", filename);
159-
return NULL;
160-
}
161-
162-
if (_fseeki64(fp, 0, SEEK_END) < 0)
163-
{
164-
WLog_WARN(TAG, "Timezone resource file '%s' seek failed", filename);
165-
goto end;
166-
}
167-
jstrlen = _ftelli64(fp);
168-
if (jstrlen < 0)
169-
{
170-
WLog_WARN(TAG, "Timezone resource file '%s' invalid length %" PRId64, filename, jstrlen);
171-
goto end;
172-
}
173-
if (_fseeki64(fp, 0, SEEK_SET) < 0)
174-
{
175-
WLog_WARN(TAG, "Timezone resource file '%s' seek failed", filename);
176-
goto end;
177-
}
178-
179-
jstr = calloc(WINPR_ASSERTING_INT_CAST(size_t, jstrlen + 1), sizeof(char));
180-
if (!jstr)
181-
{
182-
WLog_WARN(TAG, "Timezone resource file '%s' failed to allocate buffer of size %" PRId64,
183-
filename, jstrlen);
184-
goto end;
185-
}
186-
187-
if (fread(jstr, WINPR_ASSERTING_INT_CAST(size_t, jstrlen), sizeof(char), fp) != 1)
188-
{
189-
WLog_WARN(TAG, "Timezone resource file '%s' failed to read buffer of size %" PRId64,
190-
filename, jstrlen);
191-
goto end;
192-
}
193-
194-
json = WINPR_JSON_ParseWithLength(jstr, WINPR_ASSERTING_INT_CAST(size_t, jstrlen));
152+
WINPR_JSON* json = WINPR_JSON_ParseFromFile(filename);
195153
if (!json)
196154
WLog_WARN(TAG, "Timezone resource file '%s' is not a valid JSON file", filename);
197-
end:
198-
fclose(fp);
199-
free(jstr);
200155
return json;
201156
}
202157
#endif

winpr/libwinpr/utils/json/json.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <math.h>
2121
#include <errno.h>
2222

23+
#include <winpr/file.h>
2324
#include <winpr/json.h>
2425
#include <winpr/assert.h>
2526

@@ -679,3 +680,41 @@ char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
679680
return NULL;
680681
#endif
681682
}
683+
684+
WINPR_JSON* WINPR_JSON_ParseFromFile(const char* filename)
685+
{
686+
FILE* fp = winpr_fopen(filename, "r");
687+
if (!fp)
688+
return NULL;
689+
WINPR_JSON* json = WINPR_JSON_ParseFromFileFP(fp);
690+
(void)fclose(fp);
691+
return json;
692+
}
693+
694+
WINPR_JSON* WINPR_JSON_ParseFromFileFP(FILE* fp)
695+
{
696+
if (!fp)
697+
return NULL;
698+
699+
if (fseek(fp, 0, SEEK_END) != 0)
700+
return NULL;
701+
702+
const INT64 size = _ftelli64(fp);
703+
if (size < 0)
704+
return NULL;
705+
706+
if (fseek(fp, 0, SEEK_SET) != 0)
707+
return NULL;
708+
709+
const size_t usize = WINPR_ASSERTING_INT_CAST(size_t, size);
710+
char* str = calloc(usize + 1, sizeof(char));
711+
if (!str)
712+
return NULL;
713+
714+
WINPR_JSON* json = NULL;
715+
const size_t s = fread(str, sizeof(char), usize, fp);
716+
if (s == usize)
717+
json = WINPR_JSON_ParseWithLength(str, usize);
718+
free(str);
719+
return json;
720+
}

0 commit comments

Comments
 (0)