OpenVAS Manager  7.0.3~git
manage_utils.c
Go to the documentation of this file.
1 /* OpenVAS Manager
2  * $Id$
3  * Description: Module for OpenVAS Manager: Manage library utilities.
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 <assert.h>
29 
30 #include <openvas/base/openvas_hosts.h>
31 
47 long
48 time_offset (const char *zone, time_t time)
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 }
108 
116 long
117 current_offset (const char *zone)
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 }
170 
171 
175 #define MONTHS_WITHIN_YEAR() \
176  (same_month \
177  ? 0 \
178  : ((broken2->tm_mon - broken1.tm_mon) \
179  - (same_day \
180  ? (same_hour \
181  ? (same_minute \
182  ? (same_second \
183  ? 0 \
184  : (broken2->tm_sec < broken1.tm_sec)) \
185  : (broken2->tm_min < broken1.tm_min)) \
186  : (broken2->tm_hour < broken1.tm_hour)) \
187  : (broken2->tm_mday < broken1.tm_mday))))
188 
201 time_t
202 months_between (time_t time1, time_t time2)
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 }
257 
266 time_t
267 add_months (time_t time, int months)
268 {
269  struct tm *broken = localtime (&time);
270  broken->tm_mon += months;
271  return mktime (broken);
272 }
273 
286 time_t
287 next_time (time_t first, int period, int period_months, const char* timezone,
288  int periods_offset)
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 }
363 
373 int
374 manage_count_hosts_max (const char *given_hosts, const char *exclude_hosts,
375  int max_hosts)
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 }
393 
402 double
403 level_min_severity (const char *level, const char *class)
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 }
444 
453 double
454 level_max_severity (const char *level, const char *class)
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 }
495 
503 int
504 valid_db_resource_type (const char* type)
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 }
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
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
#define MONTHS_WITHIN_YEAR()
Code fragment for months_between.
Definition: manage_utils.c:175
#define SEVERITY_ERROR
Definition: manage_utils.h:38
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
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
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
#define SEVERITY_FP
Definition: manage_utils.h:34
#define SEVERITY_DEBUG
Definition: manage_utils.h:36
#define SEVERITY_UNDEFINED
Definition: manage_utils.h:42
int valid_db_resource_type(const char *type)
Check whether a resource type table name is valid.
Definition: manage_utils.c:504
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
#define SEVERITY_LOG
Definition: manage_utils.h:32