From 176a5bcf76813ab73b999885db07ec31d67cff82 Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Tue, 24 May 2016 00:54:08 +0200 Subject: [PATCH] storage: Encode SSIDs in file names if necessary SSIDs with characters other than A-Z0-9_- and space are encoded with l_util_hexstring and prefixed with a = sign to differentiate them. --- src/storage.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/storage.c b/src/storage.c index 9bc99dd9..7a98ad59 100644 --- a/src/storage.c +++ b/src/storage.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -167,6 +168,29 @@ error_create_dirs: return r; } +static char *get_network_file_path(const char *type, const char *ssid) +{ + char *path; + const char *c; + char *hex = NULL; + + for (c = ssid; *c; c++) + if (!isalnum(*c) && !strchr("-_ ", *c)) + break; + + if (*c) { + hex = l_util_hexstring((const unsigned char *) ssid, + strlen(ssid)); + + path = l_strdup_printf(STORAGEDIR "/=%s.%s", hex, type); + + l_free(hex); + } else + path = l_strdup_printf(STORAGEDIR "/%s.%s", ssid, type); + + return path; +} + struct l_settings *storage_network_open(const char *type, const char *ssid) { struct l_settings *settings; @@ -175,8 +199,7 @@ struct l_settings *storage_network_open(const char *type, const char *ssid) if (ssid == NULL || type == NULL) return NULL; - /* TODO: Handle SSID's with filesystem-reserved characters */ - path = l_strdup_printf(STORAGEDIR "/%s.%s", ssid, type); + path = get_network_file_path(type, ssid); settings = l_settings_new(); l_settings_load_from_file(settings, path); @@ -193,7 +216,7 @@ int storage_network_touch(const char *type, const char *ssid) if (ssid == NULL || type == NULL) return -EINVAL; - path = l_strdup_printf(STORAGEDIR "/%s.%s", ssid, type); + path = get_network_file_path(type, ssid); ret = utimensat(0, path, NULL, 0); l_free(path); @@ -213,7 +236,7 @@ int storage_network_get_mtime(const char *type, const char *ssid, if (ssid == NULL || type == NULL) return -EINVAL; - path = l_strdup_printf(STORAGEDIR "/%s.%s", ssid, type); + path = get_network_file_path(type, ssid); ret = stat(path, &sb); l_free(path); @@ -234,9 +257,11 @@ void storage_network_sync(const char *type, const char *ssid, { char *data; size_t length = 0; + char *path; - /* TODO: Handle SSID's with filesystem-reserved characters */ + path = get_network_file_path(type, ssid); data = l_settings_to_data(settings, &length); - write_file(data, length, STORAGEDIR "/%s.%s", ssid, type); + write_file(data, length, "%s", path); l_free(data); + l_free(path); }