Greenbone Security Assistant  8.0.0
validator.c File Reference

Validation mechanism. More...

#include "validator.h"
#include <assert.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_t * gvm_validator_rule_new (const char *regex)
 Create a new validator rule. More...
 
validator_rule_t * gvm_validator_rule_new_binary ()
 Create a new validator rule for a binary parameter. More...
 
void gvm_validator_rule_free (validator_rule_t *rule)
 Free a validator rule. More...
 
validator_t gvm_validator_new ()
 Create a new validator. More...
 
void gvm_validator_add (validator_t validator, const char *name, const char *regex)
 Add or overwrite a validation rule. More...
 
void gvm_validator_add_binary (validator_t validator, const char *name)
 Add or overwrite a validation rule for a binary data param. More...
 
int gvm_validator_alias (validator_t validator, const char *alias, const char *name)
 Make an alias for a rule name. More...
 
gchar * gvm_validator_alias_for (validator_t validator, const char *alias)
 Get the name of the rule for which a rule is an alias. More...
 
int gvm_validate (validator_t validator, const char *name, const char *value)
 Validate a string for a given rule. More...
 
void gvm_validator_free (validator_t validator)
 Free a validator. More...
 

Detailed Description

Validation mechanism.

Defines a mechanism to validate strings according to named rules.

gvm_validator_new creates a new validator which must be freed with gvm_validator_free. gvm_validator_add adds a regular expression to a validator as a rule. gvm_validate checks that a given string matches a given rule.

Definition in file validator.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "gsad vali"

GLib log domain.

Definition at line 40 of file validator.c.

Function Documentation

◆ gvm_validate()

int gvm_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 197 of file validator.c.

198 {
199  gpointer key, value_rule;
200 
201  if (name != NULL && g_utf8_validate (name, -1, NULL) == FALSE)
202  {
203  g_debug ("%s: name is not valid UTF-8", __FUNCTION__);
204  return 1;
205  }
206 
207  g_debug ("%s: name %s value %s", __FUNCTION__, name, value);
208 
209  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
210  {
211  validator_rule_t *rule;
212 
213  assert (value_rule);
214 
215  rule = (validator_rule_t *) value_rule;
216 
217  if (rule->is_binary)
218  {
219  // Skip UTF-8 and regex validation for binary data
220  return 0;
221  }
222 
223  if (value != NULL && g_utf8_validate (value, -1, NULL) == FALSE)
224  {
225  g_debug ("%s: value is not valid UTF-8", __FUNCTION__);
226  return 2;
227  }
228 
229  if (rule->regex == NULL)
230  {
231  if (value == NULL)
232  {
233  g_debug ("%s: matched, regex NULL", __FUNCTION__);
234  return 0;
235  }
236  g_debug ("%s: failed to match, regex NULL", __FUNCTION__);
237  return 2;
238  }
239 
240  if (value == NULL)
241  {
242  g_debug ("%s: failed to match, value NULL", __FUNCTION__);
243  return 2;
244  }
245 
246  g_debug ("matching <%s> against <%s>: ", (char *) rule->regex, value);
247  if (g_regex_match_simple (rule->regex, (const gchar *) value, 0, 0))
248  {
249  g_debug ("%s: matched", __FUNCTION__);
250  return 0;
251  }
252  g_debug ("%s: failed to match\n", __FUNCTION__);
253  return 2;
254  }
255 
256  g_debug ("%s: failed to find name: %s", __FUNCTION__, name);
257  return 1;
258 }
gchar * regex
Regular expression.
Definition: validator.h:41
A validator rule.
Definition: validator.h:38
gboolean is_binary
Whether to expect raw byte data, skip UTF-8 checks.
Definition: validator.h:42
validator_t validator
Parameter validator.
Definition: gsad.c:249

References validator_rule::is_binary, validator_rule::regex, and validator.

Referenced by get_user_from_connection(), handle_gmp_post(), and handle_system_report().

Here is the caller graph for this function:

◆ gvm_validator_add()

void gvm_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 116 of file validator.c.

117 {
118  g_hash_table_insert (validator, (gpointer) g_strdup (name),
119  (gpointer) gvm_validator_rule_new (regex));
120 }
validator_rule_t * gvm_validator_rule_new(const char *regex)
Create a new validator rule.
Definition: validator.c:50
validator_t validator
Parameter validator.
Definition: gsad.c:249

References gvm_validator_rule_new(), and validator.

Referenced by init_http_handlers(), and init_validator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_validator_add_binary()

void gvm_validator_add_binary ( validator_t  validator,
const char *  name 
)

Add or overwrite a validation rule for a binary data param.

Parameters
validatorValidator to add rule to.
nameName of the rule.

Definition at line 129 of file validator.c.

130 {
131  g_hash_table_insert (validator, (gpointer) g_strdup (name),
132  (gpointer) gvm_validator_rule_new_binary ());
133 }
validator_rule_t * gvm_validator_rule_new_binary()
Create a new validator rule for a binary parameter.
Definition: validator.c:68
validator_t validator
Parameter validator.
Definition: gsad.c:249

References gvm_validator_rule_new_binary(), and validator.

Referenced by init_validator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_validator_alias()

int gvm_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 145 of file validator.c.

146 {
147  gpointer key, value_rule;
148 
149  if (g_hash_table_lookup_extended (validator, name, &key, &value_rule))
150  {
151  validator_rule_t *alias_rule, *rule;
152  rule = (validator_rule_t *) value_rule;
153  alias_rule = gvm_validator_rule_new (rule->regex ? rule->regex : NULL);
154  alias_rule->alias_for = g_strdup (name);
155  g_hash_table_insert (validator, (gpointer) g_strdup (alias),
156  (gpointer) alias_rule);
157  return 0;
158  }
159  return -1;
160 }
gchar * regex
Regular expression.
Definition: validator.h:41
A validator rule.
Definition: validator.h:38
validator_rule_t * gvm_validator_rule_new(const char *regex)
Create a new validator rule.
Definition: validator.c:50
validator_t validator
Parameter validator.
Definition: gsad.c:249
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:40

References validator_rule::alias_for, gvm_validator_rule_new(), validator_rule::regex, and validator.

Referenced by init_validator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_validator_alias_for()

gchar* gvm_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 gvm_validator_free.

Definition at line 172 of file validator.c.

173 {
174  gpointer key, value_rule;
175 
176  if (g_hash_table_lookup_extended (validator, alias, &key, &value_rule))
177  {
178  validator_rule_t *rule;
179  assert (value_rule);
180  rule = (validator_rule_t *) value_rule;
181  return rule->alias_for;
182  }
183  return NULL;
184 }
A validator rule.
Definition: validator.h:38
validator_t validator
Parameter validator.
Definition: gsad.c:249
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:40

References validator_rule::alias_for, and validator.

◆ gvm_validator_free()

void gvm_validator_free ( validator_t  validator)

Free a validator.

Parameters
validatorValidator.

Definition at line 266 of file validator.c.

267 {
268  if (validator)
269  g_hash_table_destroy (validator);
270 }
validator_t validator
Parameter validator.
Definition: gsad.c:249

References validator.

Referenced by cleanup_http_handlers().

Here is the caller graph for this function:

◆ gvm_validator_new()

validator_t gvm_validator_new ( )

Create a new validator.

The validator must be freed with gvm_validator_free.

Returns
A newly allocated validator.

Definition at line 102 of file validator.c.

103 {
104  return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
105  (void (*) (gpointer)) gvm_validator_rule_free);
106 }
void gvm_validator_rule_free(validator_rule_t *rule)
Free a validator rule.
Definition: validator.c:84

References gvm_validator_rule_free().

Referenced by init_http_handlers(), and init_validator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_validator_rule_free()

void gvm_validator_rule_free ( validator_rule_t *  rule)

Free a validator rule.

Parameters
ruleValidator rule.

Definition at line 84 of file validator.c.

85 {
86  if (rule)
87  {
88  g_free (rule->alias_for);
89  g_free (rule->regex);
90  g_free (rule);
91  }
92 }
gchar * regex
Regular expression.
Definition: validator.h:41
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:40

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

Referenced by gvm_validator_new().

Here is the caller graph for this function:

◆ gvm_validator_rule_new()

validator_rule_t* gvm_validator_rule_new ( const char *  regex)

Create a new validator rule.

The validator must be freed with gvm_validator_rule_free.

Returns
A newly allocated validator rule.

Definition at line 50 of file validator.c.

51 {
52  validator_rule_t *rule;
53  rule = g_malloc (sizeof (validator_rule_t));
54  rule->regex = g_strdup (regex);
55  rule->alias_for = NULL;
56  rule->is_binary = FALSE;
57  return rule;
58 }
gchar * regex
Regular expression.
Definition: validator.h:41
A validator rule.
Definition: validator.h:38
gboolean is_binary
Whether to expect raw byte data, skip UTF-8 checks.
Definition: validator.h:42
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:40

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

Referenced by gvm_validator_add(), and gvm_validator_alias().

Here is the caller graph for this function:

◆ gvm_validator_rule_new_binary()

validator_rule_t* gvm_validator_rule_new_binary ( )

Create a new validator rule for a binary parameter.

The validator must be freed with gvm_validator_rule_free.

Returns
A newly allocated validator rule.

Definition at line 68 of file validator.c.

69 {
70  validator_rule_t *rule;
71  rule = g_malloc (sizeof (validator_rule_t));
72  rule->regex = NULL;
73  rule->alias_for = NULL;
74  rule->is_binary = TRUE;
75  return rule;
76 }
gchar * regex
Regular expression.
Definition: validator.h:41
A validator rule.
Definition: validator.h:38
gboolean is_binary
Whether to expect raw byte data, skip UTF-8 checks.
Definition: validator.h:42
gchar * alias_for
Name of the rule for which this is an alias.
Definition: validator.h:40

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

Referenced by gvm_validator_add_binary().

Here is the caller graph for this function: