Use module parameters in namedserverfilter
The namedserverfilter module now uses the module parameters. Added new server type parameter which is validated both at startup and runtime.
This commit is contained in:
parent
9b2d382232
commit
c500ddc768
@ -40,6 +40,9 @@ To use multiple filter options, list them in a comma-separated list.
|
||||
options=case,extended
|
||||
```
|
||||
|
||||
**Note:** The _ignorecase_ and _case_ options are mutually exclusive and only
|
||||
one of them should be used.
|
||||
|
||||
## Filter Parameters
|
||||
|
||||
The named server filter requires two mandatory parameters to be defined.
|
||||
|
@ -270,6 +270,26 @@ const char* config_get_string(const CONFIG_PARAMETER *params, const char *key);
|
||||
*/
|
||||
int config_get_enum(const CONFIG_PARAMETER *params, const char *key, const MXS_ENUM_VALUE *values);
|
||||
|
||||
/**
|
||||
* @brief Get a service value
|
||||
*
|
||||
* @param params List of configuration parameters
|
||||
* @param key Parameter name
|
||||
*
|
||||
* @return Pointer to configured service
|
||||
*/
|
||||
struct service* config_get_service(const CONFIG_PARAMETER *params, const char *key);
|
||||
|
||||
/**
|
||||
* @brief Get a server value
|
||||
*
|
||||
* @param params List of configuration parameters
|
||||
* @param key Parameter name
|
||||
*
|
||||
* @return Pointer to configured server
|
||||
*/
|
||||
struct server* config_get_server(const CONFIG_PARAMETER *params, const char *key);
|
||||
|
||||
/**
|
||||
* @brief Get copy of parameter value if it is defined
|
||||
*
|
||||
|
@ -77,6 +77,7 @@ enum mxs_module_param_type
|
||||
MXS_MODULE_PARAM_ENUM, /**< Enumeration of string values */
|
||||
MXS_MODULE_PARAM_PATH, /**< Path to a file or a directory */
|
||||
MXS_MODULE_PARAM_SERVICE, /**< Service name */
|
||||
MXS_MODULE_PARAM_SERVER, /**< Server name */
|
||||
};
|
||||
|
||||
/** Maximum and minimum values for integer types */
|
||||
|
@ -985,6 +985,18 @@ int config_get_enum(const CONFIG_PARAMETER *params, const char *key, const MXS_E
|
||||
return found ? rv : -1;
|
||||
}
|
||||
|
||||
SERVICE* config_get_service(const CONFIG_PARAMETER *params, const char *key)
|
||||
{
|
||||
const char *value = config_get_value_string(params, key);
|
||||
return service_find(value);
|
||||
}
|
||||
|
||||
SERVER* config_get_server(const CONFIG_PARAMETER *params, const char *key)
|
||||
{
|
||||
const char *value = config_get_value_string(params, key);
|
||||
return server_find_by_unique_name(value);
|
||||
}
|
||||
|
||||
char* config_copy_string(const CONFIG_PARAMETER *params, const char *key)
|
||||
{
|
||||
const char *value = config_get_value_string(params, key);
|
||||
@ -3171,6 +3183,14 @@ bool config_param_is_valid(const MXS_MODULE_PARAM *params, const char *key,
|
||||
}
|
||||
break;
|
||||
|
||||
case MXS_MODULE_PARAM_SERVER:
|
||||
if ((context && config_contains_type(context, value, "server")) ||
|
||||
server_find_by_unique_name(value))
|
||||
{
|
||||
valid = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case MXS_MODULE_PARAM_PATH:
|
||||
if (params[i].options & (MXS_MODULE_OPT_PATH_W_OK |
|
||||
MXS_MODULE_OPT_PATH_R_OK |
|
||||
|
@ -72,6 +72,14 @@ typedef struct
|
||||
int active; /* Is filter active */
|
||||
} REGEXHINT_SESSION;
|
||||
|
||||
static const MXS_ENUM_VALUE option_values[] =
|
||||
{
|
||||
{"ignorecase", REG_ICASE},
|
||||
{"case", 0},
|
||||
{"extended", REG_EXTENDED},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
/**
|
||||
* The module entry point routine. It is this routine that
|
||||
* must populate the structure that is referred to as the
|
||||
@ -110,6 +118,17 @@ MXS_MODULE* MXS_CREATE_MODULE()
|
||||
NULL, /* Thread init. */
|
||||
NULL, /* Thread finish. */
|
||||
{
|
||||
{"match", MXS_MODULE_PARAM_STRING, NULL, MXS_MODULE_OPT_REQUIRED},
|
||||
{"server", MXS_MODULE_PARAM_SERVER, NULL, MXS_MODULE_OPT_REQUIRED},
|
||||
{"source", MXS_MODULE_PARAM_STRING},
|
||||
{"user", MXS_MODULE_PARAM_STRING},
|
||||
{
|
||||
"options",
|
||||
MXS_MODULE_PARAM_ENUM,
|
||||
"ignorecase",
|
||||
MXS_MODULE_OPT_NONE,
|
||||
option_values
|
||||
},
|
||||
{MXS_END_MODULE_PARAMS}
|
||||
}
|
||||
};
|
||||
@ -134,80 +153,17 @@ createInstance(const char *name, char **options, CONFIG_PARAMETER *params)
|
||||
|
||||
if (my_instance)
|
||||
{
|
||||
my_instance->match = NULL;
|
||||
my_instance->server = NULL;
|
||||
my_instance->source = NULL;
|
||||
my_instance->user = NULL;
|
||||
my_instance->match = MXS_STRDUP_A(config_get_string(params, "match"));
|
||||
my_instance->server = MXS_STRDUP_A(config_get_string(params, "server"));
|
||||
my_instance->source = config_copy_string(params, "source");
|
||||
my_instance->user = config_copy_string(params, "user");
|
||||
bool error = false;
|
||||
|
||||
for (const CONFIG_PARAMETER *p = params; p; p = p->next)
|
||||
{
|
||||
if (!strcmp(p->name, "match"))
|
||||
{
|
||||
my_instance->match = MXS_STRDUP_A(p->value);
|
||||
}
|
||||
else if (!strcmp(p->name, "server"))
|
||||
{
|
||||
my_instance->server = MXS_STRDUP_A(p->value);
|
||||
}
|
||||
else if (!strcmp(p->name, "source"))
|
||||
{
|
||||
my_instance->source = MXS_STRDUP_A(p->value);
|
||||
}
|
||||
else if (!strcmp(p->name, "user"))
|
||||
{
|
||||
my_instance->user = MXS_STRDUP_A(p->value);
|
||||
}
|
||||
else if (!filter_standard_parameter(p->name))
|
||||
{
|
||||
MXS_ERROR("namedserverfilter: Unexpected parameter '%s'.", p->name);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
int cflags = config_get_enum(params, "options", option_values);
|
||||
|
||||
int cflags = REG_ICASE;
|
||||
|
||||
if (options)
|
||||
if (regcomp(&my_instance->re, my_instance->match, cflags))
|
||||
{
|
||||
for (int i = 0; options[i]; i++)
|
||||
{
|
||||
if (!strcasecmp(options[i], "ignorecase"))
|
||||
{
|
||||
cflags |= REG_ICASE;
|
||||
}
|
||||
else if (!strcasecmp(options[i], "case"))
|
||||
{
|
||||
cflags &= ~REG_ICASE;
|
||||
}
|
||||
else if (!strcasecmp(options[i], "extended"))
|
||||
{
|
||||
cflags |= REG_EXTENDED;
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("namedserverfilter: Unsupported option '%s'.",
|
||||
options[i]);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (my_instance->match == NULL)
|
||||
{
|
||||
MXS_ERROR("namedserverfilter: Missing required parameters 'match'.");
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (my_instance->server == NULL)
|
||||
{
|
||||
MXS_ERROR("namedserverfilter: Missing required parameters 'server'.");
|
||||
error = true;
|
||||
}
|
||||
if (my_instance->server && my_instance->match &&
|
||||
regcomp(&my_instance->re, my_instance->match, cflags))
|
||||
{
|
||||
MXS_ERROR("namedserverfilter: Invalid regular expression '%s'.\n",
|
||||
my_instance->match);
|
||||
MXS_ERROR("namedserverfilter: Invalid regular expression '%s'.", my_instance->match);
|
||||
MXS_FREE(my_instance->match);
|
||||
my_instance->match = NULL;
|
||||
error = true;
|
||||
@ -226,8 +182,8 @@ createInstance(const char *name, char **options, CONFIG_PARAMETER *params)
|
||||
MXS_FREE(my_instance);
|
||||
my_instance = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (FILTER *) my_instance;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user