This bug pointed out that while the keys were being checked, in some
cases it was being done a little too late. This patch makes it so if
the key is invalid, the run_uri or run_string operation isn't performed
at all and a QMF exception is raised.
Signed-off-by: Russell Bryant <rbryant(a)redhat.com>
---
src/include/matahari/sysconfig.h | 5 +--
src/lib/sysconfig.c | 63 ++++++++++++++++++++++---------------
2 files changed, 39 insertions(+), 29 deletions(-)
diff --git a/src/include/matahari/sysconfig.h b/src/include/matahari/sysconfig.h
index f19f439..3a74c61 100644
--- a/src/include/matahari/sysconfig.h
+++ b/src/include/matahari/sysconfig.h
@@ -102,10 +102,9 @@ mh_sysconfig_query(const char *query, uint32_t flags, const char
*scheme);
* \param[in] key config item to define
* \param[in] contents specifies success/fail with error where applicable
*
- * \retval 1 if writing to file suceeds
- * \retval 0 failed to write to file
+ * \return See enum mh_result
*/
-gboolean
+enum mh_result
mh_sysconfig_set_configured(const char *key, const char *contents);
/**
diff --git a/src/lib/sysconfig.c b/src/lib/sysconfig.c
index c32e498..ef192d5 100644
--- a/src/lib/sysconfig.c
+++ b/src/lib/sysconfig.c
@@ -64,43 +64,61 @@ mh_sysconfig_keys_dir_set(const char *path)
mh_string_copy(_keys_dir, path, sizeof(_keys_dir));
}
-static gboolean
-set_key(const char *key, const char *contents)
+/**
+ * \internal
+ * \brief Chcek sanity of a key
+ *
+ * \retval 0 sane
+ * \retval non-zero bonkers
+ */
+static int
+check_key_sanity(const char *key)
{
- char key_file[PATH_MAX];
- static const char valid_chars[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.";
+ static const char VALID_CHARS[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.";
char sanitized_key[PATH_MAX];
if (mh_strlen_zero(key)) {
mh_err("key cannot be empty");
- return FALSE;
+ return -1;
}
if (g_str_has_prefix(key, ".")) {
mh_err("Invalid key filename %s", key);
- return FALSE;
+ return -1;
}
mh_string_copy(sanitized_key, key, sizeof(sanitized_key));
- g_strcanon(sanitized_key, valid_chars, '!');
+ g_strcanon(sanitized_key, VALID_CHARS, '!');
if (strchr(sanitized_key, '!') != NULL) {
mh_err("Invalid key filename %s", sanitized_key);
- return FALSE;
+ return -1;
+ }
+
+ return 0;
+}
+
+static enum mh_result
+set_key(const char *key, const char *contents)
+{
+ char key_file[PATH_MAX];
+
+ if (check_key_sanity(key)) {
+ return MH_RES_INVALID_ARGS;
}
if (!g_file_test(keys_dir_get(), G_FILE_TEST_IS_DIR) &&
g_mkdir(keys_dir_get(), 0755) < 0) {
mh_err("Could not create keys directory %s", keys_dir_get());
- return FALSE;
+ return MH_RES_OTHER_ERROR;
}
-
+
g_snprintf(key_file, sizeof(key_file), "%s%s", keys_dir_get(), key);
if (!g_file_set_contents(key_file, contents, strlen(contents), NULL)) {
mh_err("Could not set file %s", key_file);
- return FALSE;
+ return MH_RES_OTHER_ERROR;
}
- return TRUE;
+ return MH_RES_SUCCESS;
}
static char *
@@ -110,8 +128,7 @@ get_key(const char *key)
char *contents = NULL;
size_t length;
- if (mh_strlen_zero(key)) {
- mh_err("key cannot be empty");
+ if (check_key_sanity(key)) {
return NULL;
}
@@ -125,14 +142,10 @@ get_key(const char *key)
return contents;
}
-int
+enum mh_result
mh_sysconfig_set_configured(const char *key, const char *contents)
{
- if (!set_key(key, contents)) {
- return FALSE;
- }
-
- return TRUE;
+ return set_key(key, contents);
}
char *
@@ -145,9 +158,8 @@ enum mh_result
mh_sysconfig_run_uri(const char *uri, uint32_t flags, const char *scheme, const char
*key,
mh_sysconfig_result_cb result_cb, void *cb_data)
{
- if (mh_strlen_zero(key)) {
- mh_err("key cannot be empty");
- return -1;
+ if (check_key_sanity(key)) {
+ return MH_RES_INVALID_ARGS;
}
return sysconfig_os_run_uri(uri, flags, scheme, key, result_cb, cb_data);
@@ -158,9 +170,8 @@ mh_sysconfig_run_string(const char *string, uint32_t flags, const char
*scheme,
const char *key, mh_sysconfig_result_cb result_cb,
void *cb_data)
{
- if (mh_strlen_zero(key)) {
- mh_err("key cannot be empty");
- return -1;
+ if (check_key_sanity(key)) {
+ return MH_RES_INVALID_ARGS;
}
return sysconfig_os_run_string(string, flags, scheme, key, result_cb, cb_data);
--
1.7.6.2