Greenbone Security Assistant  7.0.0
validator.c File Reference

Validation mechanism. More...

#include <assert.h>
#include "validator.h"
Include dependency graph for validator.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "gsad vali"
 GLib log domain. More...
 

Functions

validator_rule_topenvas_validator_rule_new (const char *regex)
 Create a new validator rule. More...
 
void openvas_validator_rule_free (validator_rule_t *rule)
 Free a validator rule. More...
 
validator_t openvas_validator_new ()
 Create a new validator. More...
 
void openvas_validator_add (validator_t validator, const char *name, const char *regex)
 Add or overwrite a validation rule. More...
 
int openvas_validator_alias (validator_t validator, const char *alias, const char *name)
 Make an alias for a rule name. More...
 
gchar * openvas_validator_alias_for (validator_t validator, const char *alias)
 Get the name of the rule for which a rule is an alias. More...
 
int openvas_validate (validator_t validator, const char *name, const char *value)
 Validate a string for a given rule. More...
 
void openvas_validator_free (validator_t validator)
 Free a validator. More...
 

Detailed Description

Validation mechanism.

Defines a mechanism to validate strings according to named rules.

openvas_validator_new creates a new validator which must be freed with openvas_validator_free. openvas_validator_add adds a regular expression to a validator as a rule. openvas_validate checks that a given string matches a given rule.

Definition in file validator.c.

Macro Definition Documentation

#define G_LOG_DOMAIN   "gsad vali"

GLib log domain.

Definition at line 35 of file validator.c.

Function Documentation

int openvas_validate ( validator_t  validator,
const char *  name,
const char *  value 
)

Validate a string for a given rule.

Parameters
validatorValidator to validate from.
nameName of rule.
valueValue to validate.
Returns
0 if valid
  • value is valid, 1 if failed to find
  • name in validator, 2 if value failed to match the regexp.

Definition at line 182 of file validator.c.

References validator_rule::regex.

Referenced by exec_omp_get(), handle_request(), and params_mhd_validate_values().

183 {
184  gpointer key, value_rule;
185 
186  if (name != NULL && g_utf8_validate (name, -1, NULL) == FALSE)
187  {
188  g_debug ("%s: name is not valid UTF-8", __FUNCTION__);
189  return 1;
190  }
191  else if (value != NULL && g_utf8_validate (value, -1, NULL) == FALSE)
192  {
193  g_debug ("%s: value is not valid UTF-8", __FUNCTION__);
194  return 2;
195  }
196 
197  g_debug ("%s: name %s value %s", __FUNCTION__, name, value);
198 
199  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
200  {
201  validator_rule_t *rule;
202 
203  assert (value_rule);
204 
205  rule = (validator_rule_t*) value_rule;
206 
207  if (rule->regex == NULL)
208  {
209  if (value == NULL)
210  {
211  g_debug ("%s: matched, regex NULL", __FUNCTION__);
212  return 0;
213  }
214  g_debug ("%s: failed to match, regex NULL", __FUNCTION__);
215  return 2;
216  }
217 
218  if (value == NULL)
219  {
220  g_debug ("%s: failed to match, value NULL", __FUNCTION__);
221  return 2;
222  }
223 
224  g_debug ("matching <%s> against <%s>: ", (char *) rule->regex, value);
225  if (g_regex_match_simple (rule->regex,
226  (const gchar *) value,
227  0,
228  0))
229  {
230  g_debug ("%s: matched", __FUNCTION__);
231  return 0;
232  }
233  g_debug ("%s: failed to match\n", __FUNCTION__);
234  return 2;
235  }
236 
237  g_debug ("%s: failed to find name: %s", __FUNCTION__, name);
238  return 1;
239 }
gchar * regex
Regular expression.
Definition: validator.h:48
A validator rule.
Definition: validator.h:45
validator_t validator
Parameter validator.
Definition: gsad.c:957

Here is the caller graph for this function:

void openvas_validator_add ( validator_t  validator,
const char *  name,
const char *  regex 
)

Add or overwrite a validation rule.

Parameters
validatorValidator to add rule to.
nameName of the rule.
regexValidation rule as a regular expression.

Definition at line 106 of file validator.c.

References openvas_validator_rule_new().

Referenced by init_validator().

109 {
110  g_hash_table_insert (validator,
111  (gpointer) g_strdup (name),
112  (gpointer) openvas_validator_rule_new (regex));
113 }
validator_t validator
Parameter validator.
Definition: gsad.c:957
validator_rule_t * openvas_validator_rule_new(const char *regex)
Create a new validator rule.
Definition: validator.c:57

Here is the call graph for this function:

Here is the caller graph for this function:

int openvas_validator_alias ( validator_t  validator,
const char *  alias,
const char *  name 
)

Make an alias for a rule name.

Parameters
validatorValidator to add alias to.
aliasName of alias for rule.
nameName of the rule.
Returns
0 success, -1 error.

Definition at line 125 of file validator.c.

References validator_rule::alias_for, openvas_validator_rule_new(), and validator_rule::regex.

Referenced by init_validator().

128 {
129  gpointer key, value_rule;
130 
131  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
132  {
133  validator_rule_t *alias_rule, *rule;
134  rule = (validator_rule_t*) value_rule;
135  alias_rule = openvas_validator_rule_new (rule->regex
136  ? rule->regex
137  : NULL);
138  alias_rule->alias_for = g_strdup (name);
139  g_hash_table_insert (validator,
140  (gpointer) g_strdup (alias),
141  (gpointer) alias_rule);
142  return 0;
143  }
144  return -1;
145 }
gchar * regex
Regular expression.
Definition: validator.h:48
A validator rule.
Definition: validator.h:45
validator_t validator
Parameter validator.
Definition: gsad.c:957
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:47
validator_rule_t * openvas_validator_rule_new(const char *regex)
Create a new validator rule.
Definition: validator.c:57

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* openvas_validator_alias_for ( validator_t  validator,
const char *  alias 
)

Get the name of the rule for which a rule is an alias.

Parameters
validatorValidator.
aliasName of alias.
Returns
Rule name if alias is an alias, else NULL. Freed by openvas_validator_free.

Definition at line 157 of file validator.c.

References validator_rule::alias_for.

Referenced by params_mhd_validate_values().

158 {
159  gpointer key, value_rule;
160 
161  if (g_hash_table_lookup_extended (validator, alias, &key, &value_rule))
162  {
163  validator_rule_t *rule;
164  assert (value_rule);
165  rule = (validator_rule_t*) value_rule;
166  return rule->alias_for;
167  }
168  return NULL;
169 }
A validator rule.
Definition: validator.h:45
validator_t validator
Parameter validator.
Definition: gsad.c:957
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:47

Here is the caller graph for this function:

void openvas_validator_free ( validator_t  validator)

Free a validator.

Parameters
validatorValidator.

Definition at line 247 of file validator.c.

248 {
249  if (validator) g_hash_table_destroy (validator);
250 }
validator_t validator
Parameter validator.
Definition: gsad.c:957
validator_t openvas_validator_new ( )

Create a new validator.

The validator must be freed with openvas_validator_free.

Returns
A newly allocated validator.

Definition at line 90 of file validator.c.

References openvas_validator_rule_free().

Referenced by init_validator().

91 {
92  return g_hash_table_new_full (g_str_hash,
93  g_str_equal,
94  g_free,
95  (void (*) (gpointer)) openvas_validator_rule_free);
96 }
void openvas_validator_rule_free(validator_rule_t *rule)
Free a validator rule.
Definition: validator.c:73

Here is the call graph for this function:

Here is the caller graph for this function:

void openvas_validator_rule_free ( validator_rule_t rule)

Free a validator rule.

Parameters
ruleValidator rule.

Definition at line 73 of file validator.c.

References validator_rule::alias_for, and validator_rule::regex.

Referenced by openvas_validator_new().

74 {
75  if (rule)
76  {
77  g_free (rule->alias_for);
78  g_free (rule->regex);
79  }
80 }
gchar * regex
Regular expression.
Definition: validator.h:48
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:47

Here is the caller graph for this function:

validator_rule_t* openvas_validator_rule_new ( const char *  regex)

Create a new validator rule.

The validator must be freed with openvas_validator_rule_free.

Returns
A newly allocated validator.

Definition at line 57 of file validator.c.

References validator_rule::alias_for, and validator_rule::regex.

Referenced by openvas_validator_add(), and openvas_validator_alias().

58 {
59  validator_rule_t *rule;
60  rule = g_malloc (sizeof (validator_rule_t));
61  rule->regex = g_strdup (regex);
62  rule->alias_for = NULL;
63  return rule;
64 }
gchar * regex
Regular expression.
Definition: validator.h:48
A validator rule.
Definition: validator.h:45
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:47

Here is the caller graph for this function: