From c6e155cf2b5faec61f2276b1a62cb6088fae36c2 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 29 Dec 2016 09:44:38 +0200 Subject: [PATCH] Move quote triming function to common place Function for removing MySQL quote characters surrounding a string moved from Cache's rules.cc to common place and renamed. The function was now moved to mysql_utils.[h|c], which is in maxscale-common, and not to the perhaps more appropriate place protocol/mysql.h, as the latter would have implied that MySQLCommon would have to be linked to, which actually might be the right thing to do. However, that has to wait until protocol/mysql.h gets an overhaul. --- include/maxscale/mysql_utils.h | 13 +++++ server/core/mysql_utils.c | 67 +++++++++++++++++++++ server/modules/filter/cache/rules.cc | 87 ++-------------------------- 3 files changed, 85 insertions(+), 82 deletions(-) diff --git a/include/maxscale/mysql_utils.h b/include/maxscale/mysql_utils.h index a03c9d2fd..568724dbb 100644 --- a/include/maxscale/mysql_utils.h +++ b/include/maxscale/mysql_utils.h @@ -31,4 +31,17 @@ char* mxs_lestr_consume(uint8_t** c, size_t *size); MYSQL *mxs_mysql_real_connect(MYSQL *mysql, SERVER *server, const char *user, const char *passwd); +/** + * Trim MySQL quote characters surrounding a string. + * + * 'abcd' => abcd + * "abcd" => abcd + * `abcd` => abcd + * + * @param s The string to be trimmed. + * + * @note The string is modified in place. + */ +bool mxs_mysql_trim_quotes(char *s); + MXS_END_DECLS diff --git a/server/core/mysql_utils.c b/server/core/mysql_utils.c index 03b3f8897..c7599d751 100644 --- a/server/core/mysql_utils.c +++ b/server/core/mysql_utils.c @@ -168,3 +168,70 @@ MYSQL *mxs_mysql_real_connect(MYSQL *con, SERVER *server, const char *user, cons return mysql_real_connect(con, server->name, user, passwd, NULL, server->port, NULL, 0); } + +bool mxs_mysql_trim_quotes(char *s) +{ + bool dequoted = true; + + char *i = s; + char *end = s + strlen(s); + + // Remove space from the beginning + while (*i && isspace(*i)) + { + ++i; + } + + if (*i) + { + // Remove space from the end + while (isspace(*(end - 1))) + { + *(end - 1) = 0; + --end; + } + + ss_dassert(end > i); + + char quote; + + switch (*i) + { + case '\'': + case '"': + case '`': + quote = *i; + ++i; + break; + + default: + quote = 0; + } + + if (quote) + { + --end; + + if (*end == quote) + { + *end = 0; + + memmove(s, i, end - i + 1); + } + else + { + dequoted = false; + } + } + else if (i != s) + { + memmove(s, i, end - i + 1); + } + } + else + { + *s = 0; + } + + return dequoted; +} diff --git a/server/modules/filter/cache/rules.cc b/server/modules/filter/cache/rules.cc index 1964411fd..dee905b55 100644 --- a/server/modules/filter/cache/rules.cc +++ b/server/modules/filter/cache/rules.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -137,7 +138,6 @@ static bool cache_rules_parse_array(CACHE_RULES *self, json_t *store, const char static bool cache_rules_parse_store_element(CACHE_RULES *self, json_t *object, size_t index); static bool cache_rules_parse_use_element(CACHE_RULES *self, json_t *object, size_t index); -static bool dequote_mysql(char *s); typedef enum pcre_quote_approach { @@ -608,7 +608,7 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut host = any; } - if (dequote_mysql(user)) + if (mxs_mysql_trim_quotes(user)) { char pcre_user[2 * len + 1]; // Surely enough @@ -621,7 +621,7 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut mysql_to_pcre(pcre_user, user, PCRE_QUOTE_VERBATIM); } - if (dequote_mysql(host)) + if (mxs_mysql_trim_quotes(host)) { char pcre_host[2 * len + 1]; // Surely enough @@ -661,12 +661,12 @@ static CACHE_RULE *cache_rule_create_simple_user(cache_rule_attribute_t attribut } else { - MXS_ERROR("Could not dequote host %s.", cvalue); + MXS_ERROR("Could not trim quotes from host %s.", cvalue); } } else { - MXS_ERROR("Could not dequote user %s.", cvalue); + MXS_ERROR("Could not trim quotes from user %s.", cvalue); } return rule; @@ -2045,83 +2045,6 @@ static bool cache_rules_parse_use_element(CACHE_RULES *self, json_t *object, siz return rule != NULL; } -/** - * Remove quote characters surrounding a string. - * 'abcd' => abcd - * "abcd" => abcd - * `abcd` => abcd - * - * @param s The string to be dequoted. - * - * @note The string is modified in place. - */ -static bool dequote_mysql(char *s) -{ - bool dequoted = true; - - char *i = s; - char *end = s + strlen(s); - - // Remove space from the beginning - while (*i && isspace(*i)) - { - ++i; - } - - if (*i) - { - // Remove space from the end - while (isspace(*(end - 1))) - { - *(end - 1) = 0; - --end; - } - - ss_dassert(end > i); - - char quote; - - switch (*i) - { - case '\'': - case '"': - case '`': - quote = *i; - ++i; - break; - - default: - quote = 0; - } - - if (quote) - { - --end; - - if (*end == quote) - { - *end = 0; - - memmove(s, i, end - i + 1); - } - else - { - dequoted = false; - } - } - else if (i != s) - { - memmove(s, i, end - i + 1); - } - } - else - { - *s = 0; - } - - return dequoted; -} - /** * Convert MySQL/MariaDB account string to a pcre compatible one. *