2018-03-14 11:27:27 +08:00
|
|
|
/* $%BEGINLICENSE%$
|
|
|
|
Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; version 2 of the
|
|
|
|
License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
|
|
|
|
|
|
|
$%ENDLICENSE%$ */
|
|
|
|
|
2018-03-06 14:00:39 +08:00
|
|
|
#include "sql-filter-variables.h"
|
|
|
|
#include "cetus-util.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "cJSON.h"
|
|
|
|
|
|
|
|
enum _value_type_t {
|
|
|
|
VAL_UNKNOWN = -1,
|
|
|
|
VAL_INT = 1,
|
|
|
|
VAL_STRING,
|
2018-03-20 14:19:44 +08:00
|
|
|
VAL_STRING_CSV, /* comma seperated string value */
|
2018-03-06 14:00:39 +08:00
|
|
|
};
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
static enum _value_type_t
|
|
|
|
value_type(const char *str)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
2018-03-20 14:19:44 +08:00
|
|
|
if (strcasecmp(str, "int") == 0)
|
|
|
|
return VAL_INT;
|
|
|
|
else if (strcasecmp(str, "string") == 0)
|
|
|
|
return VAL_STRING;
|
|
|
|
else if (strcasecmp(str, "string-csv") == 0)
|
|
|
|
return VAL_STRING_CSV;
|
|
|
|
else
|
|
|
|
return VAL_UNKNOWN;
|
2018-03-06 14:00:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sql_variable_t {
|
|
|
|
char *name;
|
|
|
|
enum _value_type_t type;
|
2018-03-20 14:19:44 +08:00
|
|
|
GList *silent_values; /* GList<char *> */
|
|
|
|
GList *allowed_values; /* GList<char *> */
|
2018-03-06 14:00:39 +08:00
|
|
|
gboolean allow_all;
|
|
|
|
gboolean silence_all;
|
|
|
|
};
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
static void
|
|
|
|
sql_variable_free(struct sql_variable_t *p)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
2018-03-20 14:19:44 +08:00
|
|
|
if (p->name)
|
|
|
|
g_free(p->name);
|
|
|
|
if (p->silent_values)
|
|
|
|
g_list_free_full(p->silent_values, g_free);
|
|
|
|
if (p->allowed_values)
|
|
|
|
g_list_free_full(p->allowed_values, g_free);
|
2018-03-06 14:00:39 +08:00
|
|
|
g_free(p);
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
static gboolean
|
|
|
|
sql_variable_is_silent_value(struct sql_variable_t *p, const char *value)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
for (l = p->silent_values; l; l = l->next) {
|
|
|
|
if (strcasecmp(l->data, value) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
static gboolean
|
|
|
|
sql_variable_is_allowed_value(struct sql_variable_t *p, const char *value)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
for (l = p->allowed_values; l; l = l->next) {
|
|
|
|
if (strcasecmp(l->data, value) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GHashTable *cetus_variables = NULL;
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
void
|
|
|
|
sql_filter_vars_destroy()
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
if (cetus_variables) {
|
|
|
|
g_hash_table_destroy(cetus_variables);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
gboolean
|
|
|
|
str_case_equal(gconstpointer v1, gconstpointer v2)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
2018-03-20 14:19:44 +08:00
|
|
|
if (!v1 || !v2)
|
|
|
|
return FALSE;
|
|
|
|
return strcasecmp((const char *)v1, (const char *)v2) == 0;
|
2018-03-06 14:00:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
guint
|
|
|
|
str_case_hash(gconstpointer v)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
2018-03-20 14:19:44 +08:00
|
|
|
char *lower = g_ascii_strdown((const char *)v, -1);
|
2018-03-06 14:00:39 +08:00
|
|
|
guint hash = g_str_hash(lower);
|
|
|
|
g_free(lower);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
gboolean
|
|
|
|
sql_filter_vars_load_str_rules(const char *json_str)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
if (!cetus_variables) {
|
|
|
|
cetus_variables = g_hash_table_new_full(str_case_hash, str_case_equal,
|
2018-03-20 14:19:44 +08:00
|
|
|
NULL, (GDestroyNotify) sql_variable_free);
|
2018-03-06 14:00:39 +08:00
|
|
|
}
|
|
|
|
cJSON *root = cJSON_Parse(json_str);
|
|
|
|
if (!root) {
|
|
|
|
g_warning(G_STRLOC ":rule file parse error");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
cJSON *var_node = cJSON_GetObjectItem(root, "variables");
|
|
|
|
if (!var_node) {
|
|
|
|
g_warning("cannot find \"variables\" json node");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
cJSON *cur = var_node->child;
|
|
|
|
for (; cur; cur = cur->next) {
|
|
|
|
cJSON *name = cJSON_GetObjectItem(cur, "name");
|
|
|
|
cJSON *type = cJSON_GetObjectItem(cur, "type");
|
|
|
|
if (!name || !type) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
struct sql_variable_t *var = g_new0(struct sql_variable_t, 1);
|
|
|
|
var->name = g_strdup(name->valuestring);
|
|
|
|
var->type = value_type(type->valuestring);
|
|
|
|
cJSON *silent_array = cJSON_GetObjectItem(cur, "silent_values");
|
|
|
|
if (silent_array) {
|
|
|
|
cJSON *silent = silent_array->child;
|
|
|
|
for (; silent; silent = silent->next) {
|
|
|
|
if (strcmp(silent->valuestring, "*") == 0) {
|
|
|
|
var->silence_all = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-20 14:19:44 +08:00
|
|
|
var->silent_values = g_list_append(var->silent_values, g_strdup(silent->valuestring));
|
2018-03-06 14:00:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cJSON *allowed_array = cJSON_GetObjectItem(cur, "allowed_values");
|
|
|
|
if (allowed_array) {
|
|
|
|
cJSON *allowed = allowed_array->child;
|
|
|
|
for (; allowed; allowed = allowed->next) {
|
|
|
|
if (strcmp(allowed->valuestring, "*") == 0) {
|
|
|
|
var->allow_all = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-20 14:19:44 +08:00
|
|
|
var->allowed_values = g_list_append(var->allowed_values, g_strdup(allowed->valuestring));
|
2018-03-06 14:00:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if duplicated, replace and free the old (key & value) */
|
|
|
|
g_hash_table_replace(cetus_variables, var->name, var);
|
|
|
|
}
|
|
|
|
cJSON_Delete(root);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
gboolean
|
|
|
|
sql_filter_vars_is_silent(const char *name, const char *val)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
if (!name) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
struct sql_variable_t *var = g_hash_table_lookup(cetus_variables, name);
|
|
|
|
if (!var) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (var->silence_all) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (!val) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (var->type) {
|
|
|
|
case VAL_STRING:
|
|
|
|
return sql_variable_is_silent_value(var, val);
|
2018-03-20 14:19:44 +08:00
|
|
|
case VAL_STRING_CSV:{
|
2018-03-06 14:00:39 +08:00
|
|
|
gchar **values = g_strsplit_set(val, ", ", -1);
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; values[i] != NULL; ++i) {
|
|
|
|
if (!sql_variable_is_silent_value(var, values[i])) {
|
|
|
|
g_strfreev(values);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(values);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
g_warning(G_STRLOC ":not implemented");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
gboolean
|
|
|
|
sql_filter_vars_is_allowed(const char *name, const char *val)
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
if (!name) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
struct sql_variable_t *var = g_hash_table_lookup(cetus_variables, name);
|
|
|
|
if (!var) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (var->allow_all) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (!val) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (var->type) {
|
|
|
|
case VAL_STRING:
|
|
|
|
return sql_variable_is_allowed_value(var, val);
|
2018-03-20 14:19:44 +08:00
|
|
|
case VAL_STRING_CSV:{
|
2018-03-06 14:00:39 +08:00
|
|
|
gchar **values = g_strsplit_set(val, ", ", -1);
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; values[i] != NULL; ++i) {
|
|
|
|
if (!sql_variable_is_allowed_value(var, values[i])) {
|
|
|
|
g_strfreev(values);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(values);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
g_warning(G_STRLOC "not implemented");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
void
|
|
|
|
sql_filter_vars_load_default_rules()
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
static const char *default_var_rule = "{"
|
|
|
|
" \"variables\": ["
|
|
|
|
" {"
|
|
|
|
" \"name\": \"sql_mode\","
|
|
|
|
" \"type\": \"string-csv\","
|
|
|
|
" \"allowed_values\": ["
|
|
|
|
" \"STRICT_TRANS_TABLES\","
|
|
|
|
" \"NO_AUTO_CREATE_USER\","
|
|
|
|
" \"NO_ENGINE_SUBSTITUTION\""
|
|
|
|
" ]"
|
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"name\": \"autocommit\","
|
|
|
|
" \"type\": \"string\","
|
|
|
|
" \"allowed_values\": [\"*\"]"
|
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"name\": \"character_set_client\","
|
|
|
|
" \"type\": \"string\","
|
|
|
|
" \"allowed_values\": [\"latin1\",\"ascii\",\"gb2312\",\"gbk\",\"utf8\",\"utf8mb4\",\"binary\",\"big5\"]"
|
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"name\": \"character_set_connection\","
|
|
|
|
" \"type\": \"string\","
|
|
|
|
" \"allowed_values\": [\"latin1\",\"ascii\",\"gb2312\",\"gbk\",\"utf8\",\"utf8mb4\",\"binary\",\"big5\"]"
|
|
|
|
" },"
|
|
|
|
" {"
|
|
|
|
" \"name\": \"character_set_results\","
|
|
|
|
" \"type\": \"string\","
|
|
|
|
" \"allowed_values\": [\"latin1\",\"ascii\",\"gb2312\",\"gbk\",\"utf8\",\"utf8mb4\",\"binary\",\"big5\",\"NULL\"]"
|
2018-03-20 14:19:44 +08:00
|
|
|
" }" " ]" "}";
|
2018-03-06 14:00:39 +08:00
|
|
|
gboolean rc = sql_filter_vars_load_str_rules(default_var_rule);
|
|
|
|
g_assert(rc);
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:19:44 +08:00
|
|
|
void
|
|
|
|
sql_filter_vars_shard_load_default_rules()
|
2018-03-06 14:00:39 +08:00
|
|
|
{
|
|
|
|
static const char *default_var_rule = "{"
|
|
|
|
" \"variables\": ["
|
|
|
|
" {"
|
|
|
|
" \"name\": \"autocommit\","
|
2018-03-20 14:19:44 +08:00
|
|
|
" \"type\": \"string\"," " \"allowed_values\": [\"*\"]" " }" " ]" "}";
|
2018-03-06 14:00:39 +08:00
|
|
|
gboolean rc = sql_filter_vars_load_str_rules(default_var_rule);
|
|
|
|
g_assert(rc);
|
|
|
|
}
|
2018-08-24 18:31:50 +08:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
sql_filter_vars_reload_str_rules(const char *json_str)
|
|
|
|
{
|
|
|
|
if (!json_str) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cetus_variables) {
|
|
|
|
g_hash_table_remove_all(cetus_variables);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql_filter_vars_load_str_rules(json_str);
|
|
|
|
}
|