OpenVAS Manager  7.0.3~git
manage_pg_server.c File Reference
#include "manage_utils.h"
#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"
#include "glib.h"
Include dependency graph for manage_pg_server.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (sql_next_time)
 
Datum sql_next_time (PG_FUNCTION_ARGS)
 Get the next time given schedule times. More...
 
 PG_FUNCTION_INFO_V1 (sql_max_hosts)
 
Datum sql_max_hosts (PG_FUNCTION_ARGS)
 Return number of hosts. More...
 
 PG_FUNCTION_INFO_V1 (sql_level_min_severity)
 
Datum sql_level_min_severity (PG_FUNCTION_ARGS)
 Return min severity of level. More...
 
 PG_FUNCTION_INFO_V1 (sql_level_max_severity)
 
Datum sql_level_max_severity (PG_FUNCTION_ARGS)
 Return max severity of level. More...
 
 PG_FUNCTION_INFO_V1 (sql_severity_matches_ov)
 
Datum sql_severity_matches_ov (PG_FUNCTION_ARGS)
 Return max severity of level. More...
 
 PG_FUNCTION_INFO_V1 (sql_valid_db_resource_type)
 
Datum sql_valid_db_resource_type (PG_FUNCTION_ARGS)
 Return max severity of level. More...
 
 PG_FUNCTION_INFO_V1 (sql_regexp)
 
Datum sql_regexp (PG_FUNCTION_ARGS)
 Return if argument 1 matches regular expression in argument 2. More...
 

Function Documentation

◆ PG_FUNCTION_INFO_V1() [1/7]

PG_FUNCTION_INFO_V1 ( sql_next_time  )

◆ PG_FUNCTION_INFO_V1() [2/7]

PG_FUNCTION_INFO_V1 ( sql_max_hosts  )

◆ PG_FUNCTION_INFO_V1() [3/7]

PG_FUNCTION_INFO_V1 ( sql_level_min_severity  )

◆ PG_FUNCTION_INFO_V1() [4/7]

PG_FUNCTION_INFO_V1 ( sql_level_max_severity  )

◆ PG_FUNCTION_INFO_V1() [5/7]

PG_FUNCTION_INFO_V1 ( sql_severity_matches_ov  )

◆ PG_FUNCTION_INFO_V1() [6/7]

PG_FUNCTION_INFO_V1 ( sql_valid_db_resource_type  )

◆ PG_FUNCTION_INFO_V1() [7/7]

PG_FUNCTION_INFO_V1 ( sql_regexp  )

◆ sql_level_max_severity()

Datum sql_level_max_severity ( PG_FUNCTION_ARGS  )

Return max severity of level.

This is a callback for a SQL function of two arguments.

Definition at line 189 of file manage_pg_server.c.

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 }
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

◆ sql_level_min_severity()

Datum sql_level_min_severity ( PG_FUNCTION_ARGS  )

Return min severity of level.

This is a callback for a SQL function of two arguments.

Definition at line 157 of file manage_pg_server.c.

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 }
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

◆ sql_max_hosts()

Datum sql_max_hosts ( PG_FUNCTION_ARGS  )

Return number of hosts.

This is a callback for a SQL function of two arguments.

Definition at line 100 of file manage_pg_server.c.

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 }
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

◆ sql_next_time()

Datum sql_next_time ( PG_FUNCTION_ARGS  )

Get the next time given schedule times.

This is a callback for a SQL function of three to five arguments.

Definition at line 61 of file manage_pg_server.c.

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 }
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

◆ sql_regexp()

Datum sql_regexp ( PG_FUNCTION_ARGS  )

Return if argument 1 matches regular expression in argument 2.

This is a callback for a SQL function of two arguments.

Definition at line 276 of file manage_pg_server.c.

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 }

◆ sql_severity_matches_ov()

Datum sql_severity_matches_ov ( PG_FUNCTION_ARGS  )

Return max severity of level.

This is a callback for a SQL function of one argument.

Definition at line 221 of file manage_pg_server.c.

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 }

◆ sql_valid_db_resource_type()

Datum sql_valid_db_resource_type ( PG_FUNCTION_ARGS  )

Return max severity of level.

This is a callback for a SQL function of one argument.

Definition at line 248 of file manage_pg_server.c.

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 }
int valid_db_resource_type(const char *)
Check whether a resource type table name is valid.
Definition: manage_utils.c:504