Greenbone Security Assistant  7.0.0
validator.c
Go to the documentation of this file.
1 /* Greenbone Security Assistant (set for openvas-libraries/base)
2  * $Id$
3  * Description: String validator.
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  * Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
8  *
9  * Copyright:
10  * Copyright (C) 2009 Greenbone Networks GmbH
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 
27 #include <assert.h>
28 
29 #include "validator.h"
30 
31 #undef G_LOG_DOMAIN
32 
35 #define G_LOG_DOMAIN "gsad vali"
36 
57 openvas_validator_rule_new (const char *regex)
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 }
65 
66 
72 void
74 {
75  if (rule)
76  {
77  g_free (rule->alias_for);
78  g_free (rule->regex);
79  }
80 }
81 
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 }
97 
105 void
107  const char *name,
108  const char *regex)
109 {
110  g_hash_table_insert (validator,
111  (gpointer) g_strdup (name),
112  (gpointer) openvas_validator_rule_new (regex));
113 }
114 
124 int
126  const char *alias,
127  const char *name)
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 }
146 
156 gchar *
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 }
170 
181 int
182 openvas_validate (validator_t validator, const char *name, const char *value)
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 }
240 
246 void
248 {
249  if (validator) g_hash_table_destroy (validator);
250 }
int openvas_validator_alias(validator_t validator, const char *alias, const char *name)
Make an alias for a rule name.
Definition: validator.c:125
gchar * regex
Regular expression.
Definition: validator.h:48
validator_t openvas_validator_new()
Create a new validator.
Definition: validator.c:90
A validator rule.
Definition: validator.h:45
void openvas_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
Definition: validator.c:106
gchar * openvas_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:157
Headers/structs for a string validator.
int openvas_validate(validator_t validator, const char *name, const char *value)
Validate a string for a given rule.
Definition: validator.c:182
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
void openvas_validator_free(validator_t validator)
Free a validator.
Definition: validator.c:247
GHashTable * validator_t
A set of name rule pairs.
Definition: validator.h:40
void openvas_validator_rule_free(validator_rule_t *rule)
Free a validator rule.
Definition: validator.c:73