OpenVAS Manager  7.0.3~git
manage_pg_server.c
Go to the documentation of this file.
1 /* OpenVAS Manager
2  * $Id$
3  * Description: Manager Manage library: Postgres server-side functions.
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2014 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #include "manage_utils.h"
27 
28 #include "postgres.h"
29 #include "fmgr.h"
30 #include "executor/spi.h"
31 #include "glib.h"
32 
33 #ifdef PG_MODULE_MAGIC
34 PG_MODULE_MAGIC;
35 #endif
36 
43 static char *
44 textndup (text *text_arg, int length)
45 {
46  char *ret;
47  ret = palloc (length + 1);
48  memcpy (ret, VARDATA (text_arg), length);
49  ret[length] = 0;
50  return ret;
51 }
52 
54 
60 Datum
61 sql_next_time (PG_FUNCTION_ARGS)
62 {
63  int32 first, period, period_months, periods_offset;
64  char *timezone;
65  int32 ret;
66 
67  first = PG_GETARG_INT32 (0);
68  period = PG_GETARG_INT32 (1);
69  period_months = PG_GETARG_INT32 (2);
70 
71  if (PG_NARGS() < 4 || PG_ARGISNULL (3))
72  timezone = NULL;
73  else
74  {
75  text* timezone_arg;
76  timezone_arg = PG_GETARG_TEXT_P (3);
77  timezone = textndup (timezone_arg, VARSIZE (timezone_arg) - VARHDRSZ);
78  }
79 
80  if (PG_NARGS() < 5 || PG_ARGISNULL (4))
81  periods_offset = 0;
82  else
83  periods_offset = PG_GETARG_INT32 (4);
84 
85  ret = next_time (first, period, period_months, timezone,
86  periods_offset);
87  if (timezone)
88  pfree (timezone);
89  PG_RETURN_INT32 (ret);
90 }
91 
93 
99 Datum
100 sql_max_hosts (PG_FUNCTION_ARGS)
101 {
102  if (PG_ARGISNULL (0))
103  PG_RETURN_INT32 (0);
104  else
105  {
106  text *hosts_arg;
107  char *hosts, *exclude;
108  int ret, max_hosts;
109 
110  hosts_arg = PG_GETARG_TEXT_P (0);
111  hosts = textndup (hosts_arg, VARSIZE (hosts_arg) - VARHDRSZ);
112  if (PG_ARGISNULL (1))
113  {
114  exclude = palloc (1);
115  exclude[0] = 0;
116  }
117  else
118  {
119  text *exclude_arg;
120  exclude_arg = PG_GETARG_TEXT_P (1);
121  exclude = textndup (exclude_arg, VARSIZE (exclude_arg) - VARHDRSZ);
122  }
123 
124  max_hosts = 4095;
125  SPI_connect ();
126  ret = SPI_exec ("SELECT coalesce ((SELECT value FROM meta"
127  " WHERE name = 'max_hosts'),"
128  " '4095');", /* Same as MANAGE_MAX_HOSTS. */
129  1); /* Max 1 row returned. */
130  if (SPI_processed > 0 && ret > 0 && SPI_tuptable != NULL)
131  {
132  char *cell;
133 
134  cell = SPI_getvalue (SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
135  elog (INFO, "cell: %s", cell);
136  if (cell)
137  max_hosts = atoi (cell);
138  }
139  elog (INFO, "done");
140  SPI_finish ();
141 
142  ret = manage_count_hosts_max (hosts, exclude, max_hosts);
143  pfree (hosts);
144  pfree (exclude);
145  PG_RETURN_INT32 (ret);
146  }
147 }
148 
150 
156 Datum
157 sql_level_min_severity (PG_FUNCTION_ARGS)
158 {
159  if (PG_ARGISNULL (0))
160  PG_RETURN_FLOAT8 (0.0);
161  else
162  {
163  text *level_arg, *class_arg;
164  char *level, *class;
165  float8 severity;
166 
167  class_arg = PG_GETARG_TEXT_P (1);
168  class = textndup (class_arg, VARSIZE (class_arg) - VARHDRSZ);
169 
170  level_arg = PG_GETARG_TEXT_P (0);
171  level = textndup (level_arg, VARSIZE (level_arg) - VARHDRSZ);
172 
173  severity = level_min_severity (level, class);
174 
175  pfree (level);
176  pfree (class);
177  PG_RETURN_FLOAT8 (severity);
178  }
179 }
180 
182 
188 Datum
189 sql_level_max_severity (PG_FUNCTION_ARGS)
190 {
191  if (PG_ARGISNULL (0))
192  PG_RETURN_FLOAT8 (0.0);
193  else
194  {
195  text *level_arg, *class_arg;
196  char *level, *class;
197  float8 severity;
198 
199  class_arg = PG_GETARG_TEXT_P (1);
200  class = textndup (class_arg, VARSIZE (class_arg) - VARHDRSZ);
201 
202  level_arg = PG_GETARG_TEXT_P (0);
203  level = textndup (level_arg, VARSIZE (level_arg) - VARHDRSZ);
204 
205  severity = level_max_severity (level, class);
206 
207  pfree (level);
208  pfree (class);
209  PG_RETURN_FLOAT8 (severity);
210  }
211 }
212 
214 
220 Datum
221 sql_severity_matches_ov (PG_FUNCTION_ARGS)
222 {
223  if (PG_ARGISNULL (0))
224  PG_RETURN_BOOL (0);
225  else if (PG_ARGISNULL (1))
226  PG_RETURN_BOOL (1);
227  else
228  {
229  float8 arg_one, arg_two;
230 
231  arg_one = PG_GETARG_FLOAT8 (0);
232  arg_two = PG_GETARG_FLOAT8 (1);
233  if (arg_one <= 0)
234  PG_RETURN_BOOL (arg_one == arg_two);
235  else
236  PG_RETURN_BOOL (arg_one >= arg_two);
237  }
238 }
239 
241 
247 Datum
248 sql_valid_db_resource_type (PG_FUNCTION_ARGS)
249 {
250  if (PG_ARGISNULL (0))
251  PG_RETURN_BOOL (0);
252  else
253  {
254  text *type_arg;
255  char *type;
256  int ret;
257 
258  type_arg = PG_GETARG_TEXT_P (0);
259  type = textndup (type_arg, VARSIZE (type_arg) - VARHDRSZ);
260 
261  ret = valid_db_resource_type (type);
262 
263  pfree (type);
264  PG_RETURN_BOOL (ret);
265  }
266 }
267 
269 
275 Datum
276 sql_regexp (PG_FUNCTION_ARGS)
277 {
278  if (PG_ARGISNULL (0) || PG_ARGISNULL (1))
279  PG_RETURN_BOOL (0);
280  else
281  {
282  text *string_arg, *regexp_arg;
283  char *string, *regexp;
284  int ret;
285 
286  ret = 0;
287 
288  regexp_arg = PG_GETARG_TEXT_P(1);
289  regexp = textndup (regexp_arg, VARSIZE (regexp_arg) - VARHDRSZ);
290 
291  string_arg = PG_GETARG_TEXT_P(0);
292  string = textndup (string_arg, VARSIZE (string_arg) - VARHDRSZ);
293 
294  if (g_regex_match_simple ((gchar *) regexp, (gchar *) string, 0, 0))
295  ret = 1;
296  else
297  ret = 0;
298 
299  pfree (string);
300  pfree (regexp);
301  PG_RETURN_BOOL (ret);
302  }
303 }
Datum sql_severity_matches_ov(PG_FUNCTION_ARGS)
Return max severity of level.
Datum sql_level_min_severity(PG_FUNCTION_ARGS)
Return min severity of level.
Datum sql_regexp(PG_FUNCTION_ARGS)
Return if argument 1 matches regular expression in argument 2.
Datum sql_valid_db_resource_type(PG_FUNCTION_ARGS)
Return max severity of level.
PG_FUNCTION_INFO_V1(sql_next_time)
double level_max_severity(const char *level, const char *class)
Get the minimum severity for a severity level and class.
Definition: manage_utils.c:454
int manage_count_hosts_max(const char *given_hosts, const char *exclude_hosts, int max_hosts)
Return number of hosts described by a hosts string.
Definition: manage_utils.c:374
Datum sql_next_time(PG_FUNCTION_ARGS)
Get the next time given schedule times.
double level_min_severity(const char *level, const char *class)
Get the minimum severity for a severity level and class.
Definition: manage_utils.c:403
Datum sql_level_max_severity(PG_FUNCTION_ARGS)
Return max severity of level.
Datum sql_max_hosts(PG_FUNCTION_ARGS)
Return number of hosts.
time_t next_time(time_t first, int period, int period_months, const char *timezone, int periods_offset)
Calculate the next time from now given a start time and a period.
Definition: manage_utils.c:287
int valid_db_resource_type(const char *)
Check whether a resource type table name is valid.
Definition: manage_utils.c:504