OpenVAS Manager  7.0.3~git
manage_utils.c File Reference

The OpenVAS Manager management library. More...

#include "manage_utils.h"
#include <assert.h>
#include <openvas/base/openvas_hosts.h>
Include dependency graph for manage_utils.c:

Go to the source code of this file.

Macros

#define MONTHS_WITHIN_YEAR()
 Code fragment for months_between. More...
 

Functions

long time_offset (const char *zone, time_t time)
 Get the offset from UTC of a timezone at a particular time. More...
 
long current_offset (const char *zone)
 Get the current offset from UTC of a timezone. More...
 
time_t months_between (time_t time1, time_t time2)
 Count number of full months between two times. More...
 
time_t add_months (time_t time, int months)
 Add months to a time. More...
 
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. More...
 
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. More...
 
double level_min_severity (const char *level, const char *class)
 Get the minimum severity for a severity level and class. More...
 
double level_max_severity (const char *level, const char *class)
 Get the minimum severity for a severity level and class. More...
 
int valid_db_resource_type (const char *type)
 Check whether a resource type table name is valid. More...
 

Detailed Description

The OpenVAS Manager management library.

Utilities used by the manage library that do not depend on anything.

Definition in file manage_utils.c.

Macro Definition Documentation

◆ MONTHS_WITHIN_YEAR

#define MONTHS_WITHIN_YEAR ( )
Value:
(same_month \
? 0 \
: ((broken2->tm_mon - broken1.tm_mon) \
- (same_day \
? (same_hour \
? (same_minute \
? (same_second \
? 0 \
: (broken2->tm_sec < broken1.tm_sec)) \
: (broken2->tm_min < broken1.tm_min)) \
: (broken2->tm_hour < broken1.tm_hour)) \
: (broken2->tm_mday < broken1.tm_mday))))

Code fragment for months_between.

Definition at line 175 of file manage_utils.c.

Function Documentation

◆ add_months()

time_t add_months ( time_t  time,
int  months 
)

Add months to a time.

Parameters
[in]timeTime.
[in]monthsMonths.
Returns
Time plus given number of months.

Definition at line 267 of file manage_utils.c.

268 {
269  struct tm *broken = localtime (&time);
270  broken->tm_mon += months;
271  return mktime (broken);
272 }

Referenced by next_time().

Here is the caller graph for this function:

◆ current_offset()

long current_offset ( const char *  zone)

Get the current offset from UTC of a timezone.

Parameters
[in]zoneTimezone, or NULL for UTC.
Returns
Seconds east of UTC.

Definition at line 117 of file manage_utils.c.

118 {
119  gchar *tz;
120  long offset;
121  time_t now;
122  struct tm *now_broken;
123 
124  if (zone == NULL)
125  return 0;
126 
127  /* Store current TZ. */
128  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
129 
130  if (setenv ("TZ", zone, 1) == -1)
131  {
132  g_warning ("%s: Failed to switch to timezone", __FUNCTION__);
133  if (tz != NULL)
134  setenv ("TZ", tz, 1);
135  g_free (tz);
136  return 0;
137  }
138 
139  tzset ();
140 
141  time (&now);
142  now_broken = localtime (&now);
143  if (setenv ("TZ", "UTC", 1) == -1)
144  {
145  g_warning ("%s: Failed to switch to UTC", __FUNCTION__);
146  if (tz != NULL)
147  setenv ("TZ", tz, 1);
148  g_free (tz);
149  return 0;
150  }
151  tzset ();
152  offset = - (now - mktime (now_broken));
153 
154  /* Revert to stored TZ. */
155  if (tz)
156  {
157  if (setenv ("TZ", tz, 1) == -1)
158  {
159  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
160  g_free (tz);
161  return 0;
162  }
163  }
164  else
165  unsetenv ("TZ");
166 
167  g_free (tz);
168  return offset;
169 }

Referenced by next_time(), and sql_current_offset().

Here is the caller graph for this function:

◆ level_max_severity()

double level_max_severity ( const char *  level,
const char *  class 
)

Get the minimum severity for a severity level and class.

Parameters
[in]levelThe name of the severity level.
[in]classThe severity class.
Returns
The minimum severity.

Definition at line 454 of file manage_utils.c.

455 {
456  if (strcasecmp (level, "Log") == 0)
457  return SEVERITY_LOG;
458  else if (strcasecmp (level, "False Positive") == 0)
459  return SEVERITY_FP;
460  else if (strcasecmp (level, "Debug") == 0)
461  return SEVERITY_DEBUG;
462  else if (strcasecmp (level, "Error") == 0)
463  return SEVERITY_ERROR;
464  else if (strcasecmp (class, "classic") == 0)
465  {
466  if (strcasecmp (level, "high") == 0)
467  return 10.0;
468  else if (strcasecmp (level, "medium") == 0)
469  return 5.0;
470  else if (strcasecmp (level, "low") == 0)
471  return 2.0;
472  else
473  return SEVERITY_UNDEFINED;
474  }
475  else if (strcasecmp (class, "pci-dss") == 0)
476  {
477  if (strcasecmp (level, "high") == 0)
478  return 10.0;
479  else
480  return SEVERITY_UNDEFINED;
481  }
482  else
483  {
484  /* NIST/BSI. */
485  if (strcasecmp (level, "high") == 0)
486  return 10.0;
487  else if (strcasecmp (level, "medium") == 0)
488  return 6.9;
489  else if (strcasecmp (level, "low") == 0)
490  return 3.9;
491  else
492  return SEVERITY_UNDEFINED;
493  }
494 }
#define SEVERITY_ERROR
Definition: manage_utils.h:38
#define SEVERITY_FP
Definition: manage_utils.h:34
#define SEVERITY_DEBUG
Definition: manage_utils.h:36
#define SEVERITY_UNDEFINED
Definition: manage_utils.h:42
#define SEVERITY_LOG
Definition: manage_utils.h:32

References SEVERITY_DEBUG, SEVERITY_ERROR, SEVERITY_FP, SEVERITY_LOG, and SEVERITY_UNDEFINED.

Referenced by severity_data_level_counts().

Here is the caller graph for this function:

◆ level_min_severity()

double level_min_severity ( const char *  level,
const char *  class 
)

Get the minimum severity for a severity level and class.

Parameters
[in]levelThe name of the severity level.
[in]classThe severity class, NULL to get from current user setting.
Returns
The minimum severity.

Definition at line 403 of file manage_utils.c.

404 {
405  if (strcasecmp (level, "Log") == 0)
406  return SEVERITY_LOG;
407  else if (strcasecmp (level, "False Positive") == 0)
408  return SEVERITY_FP;
409  else if (strcasecmp (level, "Debug") == 0)
410  return SEVERITY_DEBUG;
411  else if (strcasecmp (level, "Error") == 0)
412  return SEVERITY_ERROR;
413  else if (strcasecmp (class, "classic") == 0)
414  {
415  if (strcasecmp (level, "high") == 0)
416  return 5.1;
417  else if (strcasecmp (level, "medium") == 0)
418  return 2.1;
419  else if (strcasecmp (level, "low") == 0)
420  return 0.1;
421  else
422  return SEVERITY_UNDEFINED;
423  }
424  else if (strcasecmp (class, "pci-dss") == 0)
425  {
426  if (strcasecmp (level, "high") == 0)
427  return 4.0;
428  else
429  return SEVERITY_UNDEFINED;
430  }
431  else
432  {
433  /* NIST/BSI. */
434  if (strcasecmp (level, "high") == 0)
435  return 7.0;
436  else if (strcasecmp (level, "medium") == 0)
437  return 4.0;
438  else if (strcasecmp (level, "low") == 0)
439  return 0.1;
440  else
441  return SEVERITY_UNDEFINED;
442  }
443 }
#define SEVERITY_ERROR
Definition: manage_utils.h:38
#define SEVERITY_FP
Definition: manage_utils.h:34
#define SEVERITY_DEBUG
Definition: manage_utils.h:36
#define SEVERITY_UNDEFINED
Definition: manage_utils.h:42
#define SEVERITY_LOG
Definition: manage_utils.h:32

References SEVERITY_DEBUG, SEVERITY_ERROR, SEVERITY_FP, SEVERITY_LOG, and SEVERITY_UNDEFINED.

Referenced by severity_data_level_counts().

Here is the caller graph for this function:

◆ manage_count_hosts_max()

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.

Parameters
[in]given_hostsString describing hosts.
[in]exclude_hostsString describing hosts excluded from given set.
[in]max_hostsMax hosts.
Returns
Number of hosts, or -1 on error.

Definition at line 374 of file manage_utils.c.

376 {
377  int count;
378  openvas_hosts_t *hosts;
379 
380  hosts = openvas_hosts_new_with_max (given_hosts, max_hosts);
381  if (hosts == NULL)
382  return -1;
383 
384  if (exclude_hosts)
385  /* Don't resolve hostnames in excluded hosts. */
386  openvas_hosts_exclude (hosts, exclude_hosts, 0);
387 
388  count = openvas_hosts_count (hosts);
389  openvas_hosts_free (hosts);
390 
391  return count;
392 }

◆ months_between()

time_t months_between ( time_t  time1,
time_t  time2 
)

Count number of full months between two times.

There are two full months between 0h00.00 1 February 2010 and 0h00.00 1 April 2010. There is one full month between 0h00.00 1 February 2010 and 23h59.59 31 March 2010.

Parameters
[in]time1Earlier time.
[in]time2Later time.
Returns
Number of full months between time1 and time2.

Definition at line 202 of file manage_utils.c.

203 {
204  struct tm broken1, *broken2;
205  int same_year, same_month, same_day, same_hour, same_minute, same_second;
206  int month1_less, day1_less, hour1_less, minute1_less;
207  int second1_less;
208 
209  assert (time1 <= time2);
210 
211  localtime_r (&time1, &broken1);
212  broken2 = localtime (&time2);
213 
214  same_year = (broken1.tm_year == broken2->tm_year);
215  same_month = (broken1.tm_mon == broken2->tm_mon);
216  same_day = (broken1.tm_mday == broken2->tm_mday);
217  same_hour = (broken1.tm_hour == broken2->tm_hour);
218  same_minute = (broken1.tm_min == broken2->tm_min);
219  same_second = (broken1.tm_sec == broken2->tm_sec);
220 
221  month1_less = (broken1.tm_mon < broken2->tm_mon);
222  day1_less = (broken1.tm_mday < broken2->tm_mday);
223  hour1_less = (broken1.tm_hour < broken2->tm_hour);
224  minute1_less = (broken1.tm_min < broken2->tm_min);
225  second1_less = (broken1.tm_sec < broken2->tm_sec);
226 
227  return
228  (same_year
229  ? MONTHS_WITHIN_YEAR ()
230  : ((month1_less
231  || (same_month
232  && (day1_less
233  || (same_day
234  && (hour1_less
235  || (same_hour
236  && (minute1_less
237  || (same_minute
238  && second1_less))))))))
239  ? (/* time1 is earlier in the year than time2. */
240  ((broken2->tm_year - broken1.tm_year) * 12)
241  + MONTHS_WITHIN_YEAR ())
242  : (/* time1 is later in the year than time2. */
243  ((broken2->tm_year - broken1.tm_year - 1) * 12)
244  /* Months left in year of time1. */
245  + (11 - broken1.tm_mon)
246  /* Months past in year of time2. */
247  + broken2->tm_mon
248  /* Possible extra month due to position in month of each time. */
249  + (day1_less
250  || (same_day
251  && (hour1_less
252  || (same_hour
253  && (minute1_less
254  || (same_minute
255  && second1_less)))))))));
256 }
#define MONTHS_WITHIN_YEAR()
Code fragment for months_between.
Definition: manage_utils.c:175

References MONTHS_WITHIN_YEAR.

Referenced by next_time().

Here is the caller graph for this function:

◆ next_time()

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.

Parameters
[in]firstThe first time.
[in]periodThe period in seconds.
[in]period_monthsThe period in months.
[in]timezoneThe timezone to use.
[in]periods_offsetNumber of periods to offset. e.g. 0 = next time, -1 current/last time
Returns
the next time a schedule with the given times is due.

Definition at line 287 of file manage_utils.c.

289 {
290  int periods_diff;
291  time_t now = time (NULL);
292  long offset_diff;
293  if (timezone)
294  {
295  long first_offset_val, current_offset_val;
296  first_offset_val = time_offset (timezone, first);
297  current_offset_val = current_offset (timezone);
298  offset_diff = current_offset_val - first_offset_val;
299  }
300  else
301  {
302  offset_diff = 0;
303  }
304 
305  if (first >= now)
306  {
307  return first;
308  }
309  else if (period > 0)
310  {
311  return first
312  + ((((now - first + offset_diff) / period) + 1 + periods_offset)
313  * period)
314  - offset_diff;
315  }
316  else if (period_months > 0)
317  {
318  time_t ret;
319  gchar *tz;
320 
321  /* Store current TZ. */
322  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
323 
324  if (setenv ("TZ", timezone ? timezone : "UTC", 1) == -1)
325  {
326  g_warning ("%s: Failed to switch to timezone", __FUNCTION__);
327  if (tz != NULL)
328  setenv ("TZ", tz, 1);
329  g_free (tz);
330  return 0;
331  }
332 
333  tzset ();
334 
335  /* Calculate new time */
336  periods_diff = months_between (first, now) / period_months;
337  periods_diff += periods_offset;
338  ret = add_months (first, (periods_diff + 1) * period_months);
339  ret -= offset_diff;
340 
341  /* Revert to stored TZ. */
342  if (tz)
343  {
344  if (setenv ("TZ", tz, 1) == -1)
345  {
346  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
347  g_free (tz);
348  }
349  }
350  else
351  unsetenv ("TZ");
352 
353  g_free (tz);
354 
355  return ret;
356  }
357  else if (periods_offset == -1)
358  {
359  return first;
360  }
361  return 0;
362 }
time_t months_between(time_t time1, time_t time2)
Count number of full months between two times.
Definition: manage_utils.c:202
time_t add_months(time_t time, int months)
Add months to a time.
Definition: manage_utils.c:267
long current_offset(const char *zone)
Get the current offset from UTC of a timezone.
Definition: manage_utils.c:117
long time_offset(const char *zone, time_t time)
Get the offset from UTC of a timezone at a particular time.
Definition: manage_utils.c:48

References add_months(), current_offset(), months_between(), and time_offset().

Referenced by manage_schedule(), sql_next_time(), and task_schedule_next_time().

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

◆ time_offset()

long time_offset ( const char *  zone,
time_t  time 
)

Get the offset from UTC of a timezone at a particular time.

Parameters
[in]zoneTimezone, or NULL for UTC.
[in]timeTime.
Returns
Seconds east of UTC.

Definition at line 48 of file manage_utils.c.

49 {
50  gchar *tz;
51  struct tm *time_broken;
52  int mins;
53  char buf[100];
54 
55  if (zone == NULL || strcmp (zone, "UTC") == 0)
56  return 0;
57 
58  /* Store current TZ. */
59  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
60 
61  if (setenv ("TZ", zone, 1) == -1)
62  {
63  g_warning ("%s: Failed to switch to timezone", __FUNCTION__);
64  if (tz != NULL)
65  setenv ("TZ", tz, 1);
66  g_free (tz);
67  return 0;
68  }
69 
70  tzset ();
71 
72  time_broken = localtime (&time);
73  if (strftime (buf, 100, "%z", time_broken) == 0)
74  {
75  g_warning ("%s: Failed to format timezone", __FUNCTION__);
76  if (tz != NULL)
77  setenv ("TZ", tz, 1);
78  g_free (tz);
79  return 0;
80  }
81 
82  if (strlen (buf) >= 3)
83  {
84  mins = atoi (buf);
85  mins /= 100;
86  mins *= 60;
87  mins += atoi (buf + 3);
88  }
89  else
90  mins = 0;
91 
92  /* Revert to stored TZ. */
93  if (tz)
94  {
95  if (setenv ("TZ", tz, 1) == -1)
96  {
97  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
98  g_free (tz);
99  return mins * 60;
100  }
101  }
102  else
103  unsetenv ("TZ");
104 
105  g_free (tz);
106  return mins * 60;
107 }

Referenced by next_time().

Here is the caller graph for this function:

◆ valid_db_resource_type()

int valid_db_resource_type ( const char *  type)

Check whether a resource type table name is valid.

Parameters
[in]typeType of resource.
Returns
1 yes, 0 no.

Definition at line 504 of file manage_utils.c.

505 {
506  if (type == NULL)
507  return 0;
508 
509  return (strcasecmp (type, "agent") == 0)
510  || (strcasecmp (type, "alert") == 0)
511  || (strcasecmp (type, "config") == 0)
512  || (strcasecmp (type, "cpe") == 0)
513  || (strcasecmp (type, "credential") == 0)
514  || (strcasecmp (type, "cve") == 0)
515  || (strcasecmp (type, "cert_bund_adv") == 0)
516  || (strcasecmp (type, "dfn_cert_adv") == 0)
517  || (strcasecmp (type, "filter") == 0)
518  || (strcasecmp (type, "group") == 0)
519  || (strcasecmp (type, "host") == 0)
520  || (strcasecmp (type, "os") == 0)
521  || (strcasecmp (type, "note") == 0)
522  || (strcasecmp (type, "nvt") == 0)
523  || (strcasecmp (type, "ovaldef") == 0)
524  || (strcasecmp (type, "override") == 0)
525  || (strcasecmp (type, "port_list") == 0)
526  || (strcasecmp (type, "permission") == 0)
527  || (strcasecmp (type, "report") == 0)
528  || (strcasecmp (type, "report_format") == 0)
529  || (strcasecmp (type, "result") == 0)
530  || (strcasecmp (type, "role") == 0)
531  || (strcasecmp (type, "scanner") == 0)
532  || (strcasecmp (type, "schedule") == 0)
533  || (strcasecmp (type, "slave") == 0)
534  || (strcasecmp (type, "tag") == 0)
535  || (strcasecmp (type, "target") == 0)
536  || (strcasecmp (type, "task") == 0)
537  || (strcasecmp (type, "user") == 0);
538 }

Referenced by resource_exists(), resource_name(), resource_uuid(), and sql_resource_exists().

Here is the caller graph for this function: