The OpenVAS Manager management library.
More...
#include "manage_utils.h"
#include <assert.h>
#include <openvas/base/openvas_hosts.h>
Go to the source code of this file.
|
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...
|
|
The OpenVAS Manager management library.
Utilities used by the manage library that do not depend on anything.
Definition in file manage_utils.c.
◆ 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.
Referenced by months_between().
◆ add_months()
time_t add_months |
( |
time_t |
time, |
|
|
int |
months |
|
) |
| |
Add months to a time.
- Parameters
-
[in] | time | Time. |
[in] | months | Months. |
- Returns
- Time plus given number of months.
Definition at line 267 of file manage_utils.c.
Referenced by next_time().
269 struct tm *broken = localtime (&time);
270 broken->tm_mon += months;
271 return mktime (broken);
◆ current_offset()
long current_offset |
( |
const char * |
zone | ) |
|
Get the current offset from UTC of a timezone.
- Parameters
-
[in] | zone | Timezone, or NULL for UTC. |
- Returns
- Seconds east of UTC.
Definition at line 117 of file manage_utils.c.
Referenced by next_time(), and sql_current_offset().
122 struct tm *now_broken;
128 tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
130 if (setenv (
"TZ", zone, 1) == -1)
132 g_warning (
"%s: Failed to switch to timezone", __FUNCTION__);
134 setenv (
"TZ", tz, 1);
142 now_broken = localtime (&now);
143 if (setenv (
"TZ",
"UTC", 1) == -1)
145 g_warning (
"%s: Failed to switch to UTC", __FUNCTION__);
147 setenv (
"TZ", tz, 1);
152 offset = - (now - mktime (now_broken));
157 if (setenv (
"TZ", tz, 1) == -1)
159 g_warning (
"%s: Failed to switch to original TZ", __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] | level | The name of the severity level. |
[in] | class | The severity class. |
- Returns
- The minimum severity.
Definition at line 454 of file manage_utils.c.
References SEVERITY_DEBUG, SEVERITY_ERROR, SEVERITY_FP, SEVERITY_LOG, and SEVERITY_UNDEFINED.
Referenced by severity_data_level_counts().
456 if (strcasecmp (level,
"Log") == 0)
458 else if (strcasecmp (level,
"False Positive") == 0)
460 else if (strcasecmp (level,
"Debug") == 0)
462 else if (strcasecmp (level,
"Error") == 0)
464 else if (strcasecmp (
class,
"classic") == 0)
466 if (strcasecmp (level,
"high") == 0)
468 else if (strcasecmp (level,
"medium") == 0)
470 else if (strcasecmp (level,
"low") == 0)
475 else if (strcasecmp (
class,
"pci-dss") == 0)
477 if (strcasecmp (level,
"high") == 0)
485 if (strcasecmp (level,
"high") == 0)
487 else if (strcasecmp (level,
"medium") == 0)
489 else if (strcasecmp (level,
"low") == 0)
#define SEVERITY_UNDEFINED
◆ 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] | level | The name of the severity level. |
[in] | class | The severity class, NULL to get from current user setting. |
- Returns
- The minimum severity.
Definition at line 403 of file manage_utils.c.
References SEVERITY_DEBUG, SEVERITY_ERROR, SEVERITY_FP, SEVERITY_LOG, and SEVERITY_UNDEFINED.
Referenced by severity_data_level_counts().
405 if (strcasecmp (level,
"Log") == 0)
407 else if (strcasecmp (level,
"False Positive") == 0)
409 else if (strcasecmp (level,
"Debug") == 0)
411 else if (strcasecmp (level,
"Error") == 0)
413 else if (strcasecmp (
class,
"classic") == 0)
415 if (strcasecmp (level,
"high") == 0)
417 else if (strcasecmp (level,
"medium") == 0)
419 else if (strcasecmp (level,
"low") == 0)
424 else if (strcasecmp (
class,
"pci-dss") == 0)
426 if (strcasecmp (level,
"high") == 0)
434 if (strcasecmp (level,
"high") == 0)
436 else if (strcasecmp (level,
"medium") == 0)
438 else if (strcasecmp (level,
"low") == 0)
#define SEVERITY_UNDEFINED
◆ 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_hosts | String describing hosts. |
[in] | exclude_hosts | String describing hosts excluded from given set. |
[in] | max_hosts | Max hosts. |
- Returns
- Number of hosts, or -1 on error.
Definition at line 374 of file manage_utils.c.
378 openvas_hosts_t *hosts;
380 hosts = openvas_hosts_new_with_max (given_hosts, max_hosts);
386 openvas_hosts_exclude (hosts, exclude_hosts, 0);
388 count = openvas_hosts_count (hosts);
389 openvas_hosts_free (hosts);
◆ 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] | time1 | Earlier time. |
[in] | time2 | Later time. |
- Returns
- Number of full months between time1 and time2.
Definition at line 202 of file manage_utils.c.
References MONTHS_WITHIN_YEAR.
Referenced by next_time().
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;
209 assert (time1 <= time2);
211 localtime_r (&time1, &broken1);
212 broken2 = localtime (&time2);
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);
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);
238 && second1_less))))))))
240 ((broken2->tm_year - broken1.tm_year) * 12)
243 ((broken2->tm_year - broken1.tm_year - 1) * 12)
245 + (11 - broken1.tm_mon)
255 && second1_less)))))))));
#define MONTHS_WITHIN_YEAR()
Code fragment for months_between.
◆ 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] | first | The first time. |
[in] | period | The period in seconds. |
[in] | period_months | The period in months. |
[in] | timezone | The timezone to use. |
[in] | periods_offset | Number 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.
References add_months(), current_offset(), months_between(), and time_offset().
Referenced by manage_schedule(), sql_next_time(), and task_schedule_next_time().
291 time_t now = time (NULL);
295 long first_offset_val, current_offset_val;
298 offset_diff = current_offset_val - first_offset_val;
312 + ((((now - first + offset_diff) / period) + 1 + periods_offset)
316 else if (period_months > 0)
322 tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
324 if (setenv (
"TZ", timezone ? timezone :
"UTC", 1) == -1)
326 g_warning (
"%s: Failed to switch to timezone", __FUNCTION__);
328 setenv (
"TZ", tz, 1);
337 periods_diff += periods_offset;
338 ret =
add_months (first, (periods_diff + 1) * period_months);
344 if (setenv (
"TZ", tz, 1) == -1)
346 g_warning (
"%s: Failed to switch to original TZ", __FUNCTION__);
357 else if (periods_offset == -1)
time_t months_between(time_t time1, time_t time2)
Count number of full months between two times.
time_t add_months(time_t time, int months)
Add months to a time.
long current_offset(const char *zone)
Get the current offset from UTC of a timezone.
long time_offset(const char *zone, time_t time)
Get the offset from UTC of a timezone at a particular time.
◆ 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] | zone | Timezone, or NULL for UTC. |
[in] | time | Time. |
- Returns
- Seconds east of UTC.
Definition at line 48 of file manage_utils.c.
Referenced by next_time().
51 struct tm *time_broken;
55 if (zone == NULL || strcmp (zone,
"UTC") == 0)
59 tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
61 if (setenv (
"TZ", zone, 1) == -1)
63 g_warning (
"%s: Failed to switch to timezone", __FUNCTION__);
72 time_broken = localtime (&time);
73 if (strftime (buf, 100,
"%z", time_broken) == 0)
75 g_warning (
"%s: Failed to format timezone", __FUNCTION__);
82 if (strlen (buf) >= 3)
87 mins += atoi (buf + 3);
95 if (setenv (
"TZ", tz, 1) == -1)
97 g_warning (
"%s: Failed to switch to original TZ", __FUNCTION__);
◆ valid_db_resource_type()
int valid_db_resource_type |
( |
const char * |
type | ) |
|
Check whether a resource type table name is valid.
- Parameters
-
[in] | type | Type of resource. |
- Returns
- 1 yes, 0 no.
Definition at line 504 of file manage_utils.c.
Referenced by resource_exists(), resource_name(), resource_uuid(), and sql_resource_exists().
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);