Greenbone Security Assistant  8.0.0
validator.c
Go to the documentation of this file.
1 /* Copyright (C) 2009-2018 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
32 #include "validator.h"
33 
34 #include <assert.h>
35 
36 #undef G_LOG_DOMAIN
37 
40 #define G_LOG_DOMAIN "gsad vali"
41 
50 gvm_validator_rule_new (const char *regex)
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 }
59 
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 }
77 
83 void
85 {
86  if (rule)
87  {
88  g_free (rule->alias_for);
89  g_free (rule->regex);
90  g_free (rule);
91  }
92 }
93 
103 {
104  return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
105  (void (*) (gpointer)) gvm_validator_rule_free);
106 }
107 
115 void
116 gvm_validator_add (validator_t validator, const char *name, const char *regex)
117 {
118  g_hash_table_insert (validator, (gpointer) g_strdup (name),
119  (gpointer) gvm_validator_rule_new (regex));
120 }
121 
128 void
130 {
131  g_hash_table_insert (validator, (gpointer) g_strdup (name),
132  (gpointer) gvm_validator_rule_new_binary ());
133 }
134 
144 int
145 gvm_validator_alias (validator_t validator, const char *alias, const char *name)
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 }
161 
171 gchar *
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 }
185 
196 int
197 gvm_validate (validator_t validator, const char *name, const char *value)
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 }
259 
265 void
267 {
268  if (validator)
269  g_hash_table_destroy (validator);
270 }
validator_rule_t * gvm_validator_rule_new_binary()
Create a new validator rule for a binary parameter.
Definition: validator.c:68
int gvm_validate(validator_t validator, const char *name, const char *value)
Validate a string for a given rule.
Definition: validator.c:197
gchar * regex
Regular expression.
Definition: validator.h:41
int gvm_validator_alias(validator_t validator, const char *alias, const char *name)
Make an alias for a rule name.
Definition: validator.c:145
A validator rule.
Definition: validator.h:38
void gvm_validator_add_binary(validator_t validator, const char *name)
Add or overwrite a validation rule for a binary data param.
Definition: validator.c:129
Headers/structs for a string validator.
gboolean is_binary
Whether to expect raw byte data, skip UTF-8 checks.
Definition: validator.h:42
void gvm_validator_free(validator_t validator)
Free a validator.
Definition: validator.c:266
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
GHashTable * validator_t
A set of name rule pairs.
Definition: validator.h:33
validator_t gvm_validator_new()
Create a new validator.
Definition: validator.c:102
void gvm_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
Definition: validator.c:116
void gvm_validator_rule_free(validator_rule_t *rule)
Free a validator rule.
Definition: validator.c:84
gchar * gvm_validator_alias_for(validator_t validator, const char *alias)
Get the name of the rule for which a rule is an alias.
Definition: validator.c:172