From 5c940eda5104b440cefdbaa0bc432155c154074a Mon Sep 17 00:00:00 2001 From: David Lechevalier Date: Tue, 13 Jan 2026 16:40:14 +0100 Subject: [PATCH] [printer] Compute printer configuration file using sha256 When printer name are long, using only base64 will create a long file which can cause the issue ERROR_FILENAME_EXCED_RANGE on Windows. --- channels/printer/client/printer_main.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/channels/printer/client/printer_main.c b/channels/printer/client/printer_main.c index 439edf24b..2240e594c 100644 --- a/channels/printer/client/printer_main.c +++ b/channels/printer/client/printer_main.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -77,12 +78,29 @@ typedef enum static const char* filemap[] = { "PortDosName", "PnPName", "DriverName", "CachedPrinterConfigData" }; +WINPR_ATTR_MALLOC(free, 1) +static char* get_printer_hash(const WCHAR* name, size_t length) +{ + BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = { 0 }; + + if (!winpr_Digest(WINPR_MD_SHA256, name, length, hash, sizeof(hash))) + return NULL; + + return winpr_BinToHexString(hash, sizeof(hash), FALSE); +} + +WINPR_ATTR_MALLOC(free, 1) static char* get_printer_config_path(const rdpSettings* settings, const WCHAR* name, size_t length) { + char* config = NULL; const char* path = freerdp_settings_get_string(settings, FreeRDP_ConfigPath); char* dir = GetCombinedPath(path, "printers"); - char* bname = crypto_base64_encode((const BYTE*)name, length); - char* config = GetCombinedPath(dir, bname); + if (!dir) + return NULL; + char* bname = get_printer_hash(name, length); + if (!bname) + goto fail; + config = GetCombinedPath(dir, bname); if (config && !winpr_PathFileExists(config)) { @@ -93,6 +111,7 @@ static char* get_printer_config_path(const rdpSettings* settings, const WCHAR* n } } +fail: free(dir); free(bname); return config; @@ -101,6 +120,9 @@ static char* get_printer_config_path(const rdpSettings* settings, const WCHAR* n static BOOL printer_write_setting(const char* path, prn_conf_t type, const void* data, size_t length) { + if (!path) + return FALSE; + DWORD written = 0; BOOL rc = FALSE; HANDLE file = NULL;