Greenbone Vulnerability Management Libraries 22.7.3
nvti.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6// One of the files of gvm-libs needs to specify the meta data
7// for the doxygen documentation.
8
33/* For strptime in time.h. */
34#undef _XOPEN_SOURCE
35#define _XOPEN_SOURCE
36#include "nvti.h"
37
38#include <stdio.h> // for sscanf
39#include <string.h> // for strcmp
40#include <strings.h> // for strcasecmp
41#include <time.h> // for strptime
42
43#undef G_LOG_DOMAIN
47#define G_LOG_DOMAIN "libgvm base"
48
49/* VT references */
50
57typedef struct vtref
58{
59 gchar *type;
60 gchar *ref_id;
61 gchar *ref_text;
63
77vtref_t *
78vtref_new (const gchar *type, const gchar *ref_id, const gchar *ref_text)
79{
80 vtref_t *ref = g_malloc0 (sizeof (vtref_t));
81
82 if (type)
83 ref->type = g_strdup (type);
84 if (ref_id)
85 ref->ref_id = g_strdup (ref_id);
86 if (ref_text)
87 ref->ref_text = g_strdup (ref_text);
88
89 return ref;
90}
91
97void
99{
100 if (!ref)
101 return;
102
103 g_free (ref->type);
104 g_free (ref->ref_id);
105 g_free (ref->ref_text);
106 g_free (ref);
107}
108
117const gchar *
119{
120 return r ? r->type : NULL;
121}
122
131const gchar *
133{
134 return r ? r->ref_id : NULL;
135}
136
145const gchar *
147{
148 return r ? r->ref_text : NULL;
149}
150
151/* VT severities */
152
158typedef struct vtseverity
159{
160 gchar *type;
161 gchar *origin;
163 int date;
164 double score;
165 gchar *value;
167
182vtseverity_new (const gchar *type, const gchar *origin, int date, double score,
183 const gchar *value)
184{
185 vtseverity_t *s = g_malloc0 (sizeof (vtseverity_t));
186
187 if (type)
188 s->type = g_strdup (type);
189 if (origin)
190 s->origin = g_strdup (origin);
191 s->date = date;
192 s->score = score;
193 if (value)
194 s->value = g_strdup (value);
195
196 return s;
197}
198
204void
206{
207 if (!s)
208 return;
209
210 g_free (s->type);
211 g_free (s->origin);
212 g_free (s->value);
213 g_free (s);
214}
215
224const gchar *
226{
227 return s ? s->type : NULL;
228}
229
238const gchar *
240{
241 return s ? s->origin : NULL;
242}
243
252const gchar *
254{
255 return s ? s->value : NULL;
256}
257
266int
268{
269 return s->date;
270}
271
280double
282{
283 return s->score;
284}
285
286/* Support function for timestamps */
287
296static time_t
297parse_nvt_timestamp (const gchar *str_time)
298{
299 time_t epoch_time;
300 int offset;
301 struct tm tm;
302
303 if ((strcmp ((char *) str_time, "") == 0)
304 || (strcmp ((char *) str_time, "$Date: $") == 0)
305 || (strcmp ((char *) str_time, "$Date$") == 0)
306 || (strcmp ((char *) str_time, "$Date:$") == 0)
307 || (strcmp ((char *) str_time, "$Date") == 0)
308 || (strcmp ((char *) str_time, "$$") == 0))
309 {
310 return 0;
311 }
312
313 /* Parse the time. */
314
315 /* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
316 /* $Date: 2012-02-17 16:05:26 +0100 (Fr, 17. Feb 2012) $ */
317 /* $Date: Fri, 11 Nov 2011 14:42:28 +0100 $ */
318 memset (&tm, 0, sizeof (struct tm));
319 if (strptime ((char *) str_time, "%F %T %z", &tm) == NULL)
320 {
321 memset (&tm, 0, sizeof (struct tm));
322 if (strptime ((char *) str_time, "$Date: %F %T %z", &tm) == NULL)
323 {
324 memset (&tm, 0, sizeof (struct tm));
325 if (strptime ((char *) str_time, "%a %b %d %T %Y %z", &tm) == NULL)
326 {
327 memset (&tm, 0, sizeof (struct tm));
328 if (strptime ((char *) str_time, "$Date: %a, %d %b %Y %T %z", &tm)
329 == NULL)
330 {
331 memset (&tm, 0, sizeof (struct tm));
332 if (strptime ((char *) str_time, "$Date: %a %b %d %T %Y %z",
333 &tm)
334 == NULL)
335 {
336 g_warning ("%s: Failed to parse time: %s", __func__,
337 str_time);
338 return 0;
339 }
340 }
341 }
342 }
343 }
344 epoch_time = mktime (&tm);
345 if (epoch_time == -1)
346 {
347 g_warning ("%s: Failed to make time: %s", __func__, str_time);
348 return 0;
349 }
350
351 /* Get the timezone offset from the str_time. */
352
353 if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
354 != 1)
355 && (sscanf ((char *) str_time, "$Date: %*u-%*u-%*u %*u:%*u:%*u %d%*[^]]",
356 &offset)
357 != 1)
358 && (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
359 &offset)
360 != 1)
361 && (sscanf ((char *) str_time,
362 "$Date: %*s %*s %*s %*u %*u:%*u:%*u %d%*[^]]", &offset)
363 != 1)
364 && (sscanf ((char *) str_time,
365 "$Date: %*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]", &offset)
366 != 1))
367 {
368 g_warning ("%s: Failed to parse timezone offset: %s", __func__, str_time);
369 return 0;
370 }
371
372 /* Use the offset to convert to UTC. */
373
374 if (offset < 0)
375 {
376 epoch_time += ((-offset) / 100) * 60 * 60;
377 epoch_time += ((-offset) % 100) * 60;
378 }
379 else if (offset > 0)
380 {
381 epoch_time -= (offset / 100) * 60 * 60;
382 epoch_time -= (offset % 100) * 60;
383 }
384
385 return epoch_time;
386}
387
388/* VT Information */
389
393typedef struct nvti
394{
395 gchar *oid;
396 gchar *name;
398 gchar *summary;
399 gchar *insight;
400 gchar *affected;
401 gchar *impact;
406 gchar *solution;
410 gchar *tag;
411 gchar *cvss_base;
418 gchar
421 gchar *detection;
422 gchar *qod_type;
423 gchar *qod;
425 GSList *refs;
426 GSList *severities;
427 GSList *prefs;
429 // The following are not settled yet.
430 gint category;
431 gchar *family;
433
443int
445{
446 if (!vt)
447 return -1;
448
449 vt->refs = g_slist_append (vt->refs, ref);
450 return 0;
451}
452
462int
464{
465 if (!vt)
466 return -1;
467
468 vt->severities = g_slist_append (vt->severities, s);
469 return 0;
470}
471
472/* VT preferences */
473
477typedef struct nvtpref
478{
479 int id;
480 gchar *type;
481 gchar *name;
482 gchar *dflt;
484
500nvtpref_t *
501nvtpref_new (int id, const gchar *name, const gchar *type, const gchar *dflt)
502{
503 nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
504
505 np->id = id;
506 if (name)
507 np->name = g_strdup (name);
508 if (type)
509 np->type = g_strdup (type);
510 if (dflt)
511 np->dflt = g_strdup (dflt);
512
513 return np;
514}
515
521void
523{
524 if (!np)
525 return;
526
527 g_free (np->name);
528 g_free (np->type);
529 g_free (np->dflt);
530 g_free (np);
531}
532
541int
543{
544 return np ? np->id : -1;
545}
546
555gchar *
557{
558 return np ? np->name : NULL;
559}
560
569gchar *
571{
572 return np ? np->type : NULL;
573}
574
583gchar *
585{
586 return np ? np->dflt : NULL;
587}
588
597nvti_t *
599{
600 return (nvti_t *) g_malloc0 (sizeof (nvti_t));
601}
602
608void
610{
611 if (!n)
612 return;
613
614 g_free (n->oid);
615 g_free (n->name);
616 g_free (n->summary);
617 g_free (n->insight);
618 g_free (n->affected);
619 g_free (n->impact);
620 g_free (n->solution);
621 g_free (n->solution_type);
622 g_free (n->solution_method);
623 g_free (n->tag);
624 g_free (n->cvss_base);
625 g_free (n->dependencies);
626 g_free (n->required_keys);
627 g_free (n->mandatory_keys);
628 g_free (n->excluded_keys);
629 g_free (n->required_ports);
630 g_free (n->required_udp_ports);
631 g_free (n->detection);
632 g_free (n->qod_type);
633 g_free (n->qod);
634 g_free (n->family);
635 g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
636 g_slist_free_full (n->severities, (void (*) (void *)) vtseverity_free);
637 g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
638 g_free (n);
639}
640
649gchar *
650nvti_oid (const nvti_t *n)
651{
652 return n ? n->oid : NULL;
653}
654
663gchar *
665{
666 return n ? n->name : NULL;
667}
668
677gchar *
679{
680 return n ? n->summary : NULL;
681}
682
691gchar *
693{
694 return n ? n->insight : NULL;
695}
696
705gchar *
707{
708 return n ? n->affected : NULL;
709}
710
719gchar *
721{
722 return n ? n->impact : NULL;
723}
724
733time_t
735{
736 return n ? n->creation_time : 0;
737}
738
747time_t
749{
750 return n ? n->modification_time : 0;
751}
752
760guint
762{
763 return n ? g_slist_length (n->refs) : 0;
764}
765
775vtref_t *
776nvti_vtref (const nvti_t *n, guint p)
777{
778 return n ? g_slist_nth_data (n->refs, p) : NULL;
779}
780
804gchar *
805nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
806 guint use_types)
807{
808 gchar *refs, *refs2, **exclude_item;
809 vtref_t *ref;
810 guint i, exclude;
811 gchar **exclude_split;
812
813 if (!n)
814 return NULL;
815
816 refs = NULL;
817 refs2 = NULL;
818 exclude = 0;
819
820 if (exclude_types && exclude_types[0])
821 exclude_split = g_strsplit (exclude_types, ",", 0);
822 else
823 exclude_split = NULL;
824
825 for (i = 0; i < g_slist_length (n->refs); i++)
826 {
827 ref = g_slist_nth_data (n->refs, i);
828 if (type && strcasecmp (ref->type, type) != 0)
829 continue;
830
831 if (exclude_split)
832 {
833 exclude = 0;
834 for (exclude_item = exclude_split; *exclude_item; exclude_item++)
835 {
836 if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
837 {
838 exclude = 1;
839 break;
840 }
841 }
842 }
843
844 if (!exclude)
845 {
846 if (use_types)
847 {
848 if (refs)
849 refs2 =
850 g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
851 else
852 refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
853 }
854 else
855 {
856 if (refs)
857 refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
858 else
859 refs2 = g_strdup_printf ("%s", ref->ref_id);
860 }
861 g_free (refs);
862 refs = refs2;
863 }
864 }
865
866 g_strfreev (exclude_split);
867
868 return refs;
869}
870
878guint
880{
881 return n ? g_slist_length (n->severities) : 0;
882}
883
894nvti_vtseverity (const nvti_t *n, guint p)
895{
896 return n ? g_slist_nth_data (n->severities, p) : NULL;
897}
898
906double
908{
909 unsigned int i;
910 double score = -1.0;
911
912 for (i = 0; i < nvti_vtseverities_len (n); i++)
913 {
914 vtseverity_t *severity;
915
916 severity = nvti_vtseverity (n, i);
917 if (vtseverity_score (severity) > score)
918 score = vtseverity_score (severity);
919 }
920
921 return score;
922}
923
938gchar *
940{
941 gchar *vector;
942
943 /* Currently, only one severity_vector can be stored as tag.
944 * Therfore we just check this one. */
945 vector = nvti_get_tag (n, "severity_vector");
946 if (vector)
947 return vector;
948
949 vector = nvti_get_tag (n, "cvss_base_vector");
950
951 return vector;
952}
953
962gchar *
964{
965 return n ? n->solution : NULL;
966}
967
976gchar *
978{
979 return n ? n->solution_type : NULL;
980}
981
990gchar *
992{
993 return n ? n->solution_method : NULL;
994}
995
1004gchar *
1006{
1007 return n ? n->tag : NULL;
1008}
1009
1020gchar *
1021nvti_get_tag (const nvti_t *n, const gchar *name)
1022{
1023 gchar **split, **point;
1024
1025 if (!n || n->tag == NULL || !name)
1026 return NULL;
1027
1028 split = g_strsplit (n->tag, "|", 0);
1029 point = split;
1030
1031 while (*point)
1032 {
1033 if ((strlen (*point) > strlen (name))
1034 && (strncmp (*point, name, strlen (name)) == 0)
1035 && ((*point)[strlen (name)] == '='))
1036 {
1037 gchar *ret;
1038 ret = g_strdup (*point + strlen (name) + 1);
1039 g_strfreev (split);
1040 return ret;
1041 }
1042 point++;
1043 }
1044 g_strfreev (split);
1045 return NULL;
1046}
1047
1056gchar *
1058{
1059 return n ? n->cvss_base : NULL;
1060}
1061
1070gchar *
1072{
1073 return n ? n->dependencies : NULL;
1074}
1075
1084gchar *
1086{
1087 return n ? n->required_keys : NULL;
1088}
1089
1098gchar *
1100{
1101 return n ? n->mandatory_keys : NULL;
1102}
1103
1112gchar *
1114{
1115 return n ? n->excluded_keys : NULL;
1116}
1117
1126gchar *
1128{
1129 return n ? n->required_ports : NULL;
1130}
1131
1140gchar *
1142{
1143 return n ? n->required_udp_ports : NULL;
1144}
1145
1154gchar *
1156{
1157 return n ? n->detection : NULL;
1158}
1159
1168gchar *
1170{
1171 return n ? n->qod_type : NULL;
1172}
1173
1182gchar *
1184{
1185 return n ? n->qod : NULL;
1186}
1187
1196gchar *
1198{
1199 return n ? n->family : NULL;
1200}
1201
1209guint
1211{
1212 return n ? g_slist_length (n->prefs) : 0;
1213}
1214
1224const nvtpref_t *
1225nvti_pref (const nvti_t *n, guint p)
1226{
1227 return n ? g_slist_nth_data (n->prefs, p) : NULL;
1228}
1229
1237gint
1239{
1240 return n ? n->category : -1;
1241}
1242
1252int
1253nvti_set_oid (nvti_t *n, const gchar *oid)
1254{
1255 if (!n)
1256 return -1;
1257
1258 g_free (n->oid);
1259 n->oid = g_strdup (oid);
1260 return 0;
1261}
1262
1272int
1273nvti_set_name (nvti_t *n, const gchar *name)
1274{
1275 if (!n)
1276 return -1;
1277
1278 g_free (n->name);
1279 n->name = g_strdup (name);
1280 return 0;
1281}
1282
1292int
1293nvti_put_name (nvti_t *n, gchar *name)
1294{
1295 if (!n)
1296 return -1;
1297
1298 g_free (n->name);
1299 n->name = name;
1300 return 0;
1301}
1302
1312int
1313nvti_set_summary (nvti_t *n, const gchar *summary)
1314{
1315 if (!n)
1316 return -1;
1317
1318 g_free (n->summary);
1319 n->summary = g_strdup (summary);
1320 return 0;
1321}
1322
1332int
1333nvti_put_summary (nvti_t *n, gchar *summary)
1334{
1335 if (!n)
1336 return -1;
1337
1338 g_free (n->summary);
1339 n->summary = summary;
1340 return 0;
1341}
1342
1352int
1353nvti_set_insight (nvti_t *n, const gchar *insight)
1354{
1355 if (!n)
1356 return -1;
1357
1358 g_free (n->insight);
1359 n->insight = g_strdup (insight);
1360 return 0;
1361}
1362
1372int
1373nvti_put_insight (nvti_t *n, gchar *insight)
1374{
1375 if (!n)
1376 return -1;
1377
1378 g_free (n->insight);
1379 n->insight = insight;
1380 return 0;
1381}
1382
1392int
1393nvti_set_affected (nvti_t *n, const gchar *affected)
1394{
1395 if (!n)
1396 return -1;
1397
1398 g_free (n->affected);
1399 n->affected = g_strdup (affected);
1400 return 0;
1401}
1402
1412int
1413nvti_put_affected (nvti_t *n, gchar *affected)
1414{
1415 if (!n)
1416 return -1;
1417
1418 g_free (n->affected);
1419 n->affected = affected;
1420 return 0;
1421}
1422
1432int
1433nvti_set_impact (nvti_t *n, const gchar *impact)
1434{
1435 if (!n)
1436 return -1;
1437
1438 g_free (n->impact);
1439 n->impact = g_strdup (impact);
1440 return 0;
1441}
1442
1452int
1453nvti_put_impact (nvti_t *n, gchar *impact)
1454{
1455 if (!n)
1456 return -1;
1457
1458 g_free (n->impact);
1459 n->impact = impact;
1460 return 0;
1461}
1462
1472int
1473nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1474{
1475 if (!n)
1476 return -1;
1477
1478 n->creation_time = creation_time;
1479 return 0;
1480}
1481
1491int
1492nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1493{
1494 if (!n)
1495 return -1;
1496
1497 n->modification_time = modification_time;
1498 return 0;
1499}
1500
1510int
1511nvti_set_solution (nvti_t *n, const gchar *solution)
1512{
1513 if (!n)
1514 return -1;
1515
1516 g_free (n->solution);
1517 n->solution = g_strdup (solution);
1518 return 0;
1519}
1520
1530int
1531nvti_put_solution (nvti_t *n, gchar *solution)
1532{
1533 if (!n)
1534 return -1;
1535
1536 g_free (n->solution);
1537 n->solution = solution;
1538 return 0;
1539}
1540
1551int
1552nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1553{
1554 if (!n)
1555 return -1;
1556
1557 g_free (n->solution_type);
1558 n->solution_type = g_strdup (solution_type);
1559 return 0;
1560}
1561
1572int
1573nvti_set_solution_method (nvti_t *n, const gchar *solution_method)
1574{
1575 if (!n)
1576 return -1;
1577
1578 g_free (n->solution_method);
1579 n->solution_method = g_strdup (solution_method);
1580 return 0;
1581}
1582
1599int
1600nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1601{
1602 gchar *newvalue = NULL;
1603
1604 if (!n)
1605 return -1;
1606
1607 if (!name || !name[0])
1608 return -2;
1609
1610 if (!value || !value[0])
1611 return -3;
1612
1613 if (!strcmp (name, "last_modification"))
1614 {
1616 newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1617 }
1618 else if (!strcmp (name, "creation_date"))
1619 {
1621 newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1622 }
1623 else if (!strcmp (name, "severity_date"))
1624 newvalue = g_strdup_printf ("%i", (int) parse_nvt_timestamp (value));
1625 else if (!strcmp (name, "cvss_base"))
1626 {
1627 /* Ignore this tag because it is not being used.
1628 * It is redundant with the tag cvss_base_vector from which
1629 * it is computed.
1630 * Once GOS 6 and GVM 11 are retired, all set_tag commands
1631 * in the NASL scripts can be removed that set "cvss_base".
1632 * Once this happened this exception can be removed from the code.
1633 */
1634 return 0;
1635 }
1636
1637 if (n->tag)
1638 {
1639 gchar *newtag;
1640
1641 newtag =
1642 g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1643 g_free (n->tag);
1644 n->tag = newtag;
1645 }
1646 else
1647 n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1648
1649 g_free (newvalue);
1650
1651 return 0;
1652}
1653
1663int
1664nvti_set_tag (nvti_t *n, const gchar *tag)
1665{
1666 if (!n)
1667 return -1;
1668
1669 g_free (n->tag);
1670 if (tag && tag[0])
1671 n->tag = g_strdup (tag);
1672 else
1673 n->tag = NULL;
1674 return 0;
1675}
1676
1686int
1687nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1688{
1689 if (!n)
1690 return -1;
1691
1692 g_free (n->cvss_base);
1693 if (cvss_base && cvss_base[0])
1694 n->cvss_base = g_strdup (cvss_base);
1695 else
1696 n->cvss_base = NULL;
1697 return 0;
1698}
1699
1710int
1711nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1712{
1713 if (!n)
1714 return -1;
1715
1716 g_free (n->dependencies);
1717 if (dependencies && dependencies[0])
1718 n->dependencies = g_strdup (dependencies);
1719 else
1720 n->dependencies = NULL;
1721 return 0;
1722}
1723
1734int
1735nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1736{
1737 if (!n)
1738 return -1;
1739
1740 g_free (n->required_keys);
1741 if (required_keys && required_keys[0])
1742 n->required_keys = g_strdup (required_keys);
1743 else
1744 n->required_keys = NULL;
1745 return 0;
1746}
1747
1758int
1759nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1760{
1761 if (!n)
1762 return -1;
1763
1764 g_free (n->mandatory_keys);
1765 if (mandatory_keys && mandatory_keys[0])
1766 n->mandatory_keys = g_strdup (mandatory_keys);
1767 else
1768 n->mandatory_keys = NULL;
1769 return 0;
1770}
1771
1782int
1783nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1784{
1785 if (!n)
1786 return -1;
1787
1788 g_free (n->excluded_keys);
1789 if (excluded_keys && excluded_keys[0])
1790 n->excluded_keys = g_strdup (excluded_keys);
1791 else
1792 n->excluded_keys = NULL;
1793 return 0;
1794}
1795
1806int
1807nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1808{
1809 if (!n)
1810 return -1;
1811
1812 g_free (n->required_ports);
1813 if (required_ports && required_ports[0])
1814 n->required_ports = g_strdup (required_ports);
1815 else
1816 n->required_ports = NULL;
1817 return 0;
1818}
1819
1830int
1831nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1832{
1833 if (!n)
1834 return -1;
1835
1836 g_free (n->required_udp_ports);
1837 if (required_udp_ports && required_udp_ports[0])
1838 n->required_udp_ports = g_strdup (required_udp_ports);
1839 else
1840 n->required_udp_ports = NULL;
1841 return 0;
1842}
1843
1853int
1854nvti_set_detection (nvti_t *n, const gchar *detection)
1855{
1856 if (!n)
1857 return -1;
1858
1859 g_free (n->detection);
1860 n->detection = g_strdup (detection);
1861 return 0;
1862}
1863
1873int
1874nvti_put_detection (nvti_t *n, gchar *detection)
1875{
1876 if (!n)
1877 return -1;
1878
1879 g_free (n->detection);
1880 n->detection = detection;
1881 return 0;
1882}
1883
1894int
1895nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1896{
1897 if (!n)
1898 return -1;
1899
1900 g_free (n->qod_type);
1901 if (qod_type && qod_type[0])
1902 n->qod_type = g_strdup (qod_type);
1903 else
1904 n->qod_type = NULL;
1905 return 0;
1906}
1907
1918int
1919nvti_set_qod (nvti_t *n, const gchar *qod)
1920{
1921 if (!n)
1922 return -1;
1923
1924 g_free (n->qod);
1925 if (qod && qod[0])
1926 n->qod = g_strdup (qod);
1927 else
1928 n->qod = NULL;
1929 return 0;
1930}
1931
1941int
1942nvti_set_family (nvti_t *n, const gchar *family)
1943{
1944 if (!n)
1945 return -1;
1946
1947 g_free (n->family);
1948 n->family = g_strdup (family);
1949 return 0;
1950}
1951
1961int
1962nvti_put_family (nvti_t *n, gchar *family)
1963{
1964 if (!n)
1965 return -1;
1966
1967 g_free (n->family);
1968 n->family = family;
1969 return 0;
1970}
1971
1981int
1982nvti_set_category (nvti_t *n, const gint category)
1983{
1984 if (!n)
1985 return -1;
1986
1987 n->category = category;
1988 return 0;
1989}
1990
2006int
2007nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
2008 const gchar *ref_text)
2009{
2010 gchar **split, **item;
2011
2012 if (!n)
2013 return 1;
2014
2015 if (!ref_ids)
2016 return 2;
2017
2018 split = g_strsplit (ref_ids, ",", 0);
2019
2020 for (item = split; *item; item++)
2021 {
2022 gchar *id;
2023
2024 id = *item;
2025 g_strstrip (id);
2026
2027 if (strcmp (id, "") == 0)
2028 continue;
2029
2030 if (type)
2031 {
2032 nvti_add_vtref (n, vtref_new (type, id, ref_text));
2033 }
2034 else
2035 {
2036 gchar **split2;
2037
2038 split2 = g_strsplit (id, ":", 2);
2039 if (split2[0] && split2[1])
2040 nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
2041 g_strfreev (split2);
2042 }
2043 }
2044 g_strfreev (split);
2045
2046 return 0;
2047}
2048
2058int
2059nvti_add_required_keys (nvti_t *n, const gchar *key)
2060{
2061 gchar *old;
2062
2063 if (!n)
2064 return 1;
2065 if (!key)
2066 return 2;
2067
2068 old = n->required_keys;
2069
2070 if (old)
2071 {
2072 n->required_keys = g_strdup_printf ("%s, %s", old, key);
2073 g_free (old);
2074 }
2075 else
2076 n->required_keys = g_strdup (key);
2077
2078 return 0;
2079}
2080
2090int
2091nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
2092{
2093 gchar *old;
2094
2095 if (!n)
2096 return 1;
2097 if (!key)
2098 return 2;
2099
2100 old = n->mandatory_keys;
2101
2102 if (old)
2103 {
2104 n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
2105 g_free (old);
2106 }
2107 else
2108 n->mandatory_keys = g_strdup (key);
2109
2110 return 0;
2111}
2112
2122int
2123nvti_add_excluded_keys (nvti_t *n, const gchar *key)
2124{
2125 gchar *old;
2126
2127 if (!n)
2128 return 1;
2129 if (!key)
2130 return 2;
2131
2132 old = n->excluded_keys;
2133
2134 if (old)
2135 {
2136 n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
2137 g_free (old);
2138 }
2139 else
2140 n->excluded_keys = g_strdup (key);
2141
2142 return 0;
2143}
2144
2154int
2155nvti_add_required_ports (nvti_t *n, const gchar *port)
2156{
2157 gchar *old;
2158
2159 if (!n)
2160 return 1;
2161 if (!port)
2162 return 2;
2163
2164 old = n->required_ports;
2165
2166 if (old)
2167 {
2168 n->required_ports = g_strdup_printf ("%s, %s", old, port);
2169 g_free (old);
2170 }
2171 else
2172 n->required_ports = g_strdup (port);
2173
2174 return 0;
2175}
2176
2186int
2188{
2189 gchar *old;
2190
2191 if (!n)
2192 return 1;
2193 if (!port)
2194 return 2;
2195
2196 old = n->required_udp_ports;
2197
2198 if (old)
2199 {
2200 n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
2201 g_free (old);
2202 }
2203 else
2204 n->required_udp_ports = g_strdup (port);
2205
2206 return 0;
2207}
2208
2218int
2220{
2221 if (!n)
2222 return -1;
2223
2224 n->prefs = g_slist_append (n->prefs, np);
2225 return 0;
2226}
2227
2228/* Collections of nvtis. */
2229
2235static void
2237{
2238 nvti_free ((nvti_t *) nvti);
2239}
2240
2246nvtis_t *
2248{
2249 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2251}
2252
2258void
2260{
2261 if (nvtis)
2262 g_hash_table_destroy (nvtis);
2263}
2264
2271void
2273{
2274 if (nvti)
2275 g_hash_table_insert (
2276 nvtis, (gpointer) (nvti_oid (nvti) ? g_strdup (nvti_oid (nvti)) : NULL),
2277 (gpointer) nvti);
2278}
2279
2288nvti_t *
2289nvtis_lookup (nvtis_t *nvtis, const char *oid)
2290{
2291 return g_hash_table_lookup (nvtis, oid);
2292}
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:776
struct vtseverity vtseverity_t
The structure for a severity of a VT.
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition nvti.c:598
int nvti_put_impact(nvti_t *n, gchar *impact)
Set the impact text of a NVT, using the given memory.
Definition nvti.c:1453
void vtseverity_free(vtseverity_t *s)
Free memory of a vtseverity structure.
Definition nvti.c:205
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition nvti.c:1071
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition nvti.c:2059
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition nvti.c:1210
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1919
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition nvti.c:1155
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition nvti.c:1141
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1511
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:1021
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition nvti.c:678
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1552
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition nvti.c:1127
int nvti_put_insight(nvti_t *n, gchar *insight)
Set the insight text of a NVT, using the given memory.
Definition nvti.c:1373
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:805
double nvti_severity_score(const nvti_t *n)
Get the maximum severity score.
Definition nvti.c:907
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition nvti.c:570
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition nvti.c:1783
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
int nvti_set_qod_type(nvti_t *n, const gchar *qod_type)
Set the QoD type of a NVT.
Definition nvti.c:1895
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition nvti.c:2123
int nvti_put_detection(nvti_t *n, gchar *detection)
Set the detection text of a NVT, using the given memory.
Definition nvti.c:1874
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2247
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition nvti.c:1711
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition nvti.c:1807
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1253
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition nvti.c:2187
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1664
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1687
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition nvti.c:1759
int nvti_put_name(nvti_t *n, gchar *name)
Set the name of a NVT, using the given memory.
Definition nvti.c:1293
int nvti_put_family(nvti_t *n, gchar *family)
Set the family of a NVT, using the given memory.
Definition nvti.c:1962
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition nvti.c:1238
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1433
const gchar * vtseverity_type(const vtseverity_t *s)
Get the type of a severity.
Definition nvti.c:225
int nvti_add_required_ports(nvti_t *n, const gchar *port)
Add a required port of a NVT.
Definition nvti.c:2155
struct nvtpref nvtpref_t
The structure for a preference of a NVT.
gchar * nvti_family(const nvti_t *n)
Get the family name.
Definition nvti.c:1197
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:939
struct vtref vtref_t
The structure for a cross reference of a VT.
static void free_nvti_for_hash_table(gpointer nvti)
Free an NVT Info, for g_hash_table_destroy.
Definition nvti.c:2236
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:991
guint nvti_vtseverities_len(const nvti_t *n)
Get the number of severities of the NVT.
Definition nvti.c:879
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition nvti.c:1225
gchar * nvti_qod(const nvti_t *n)
Get the QoD.
Definition nvti.c:1183
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition nvti.c:1113
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1313
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:664
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:650
nvtpref_t * nvtpref_new(int id, const gchar *name, const gchar *type, const gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition nvti.c:501
int nvti_put_affected(nvti_t *n, gchar *affected)
Set the affected text of a NVT, using the given memory.
Definition nvti.c:1413
int nvti_put_solution(nvti_t *n, gchar *solution)
Set the solution of a NVT, using the given memory.
Definition nvti.c:1531
double vtseverity_score(const vtseverity_t *s)
Get the score of a severity.
Definition nvti.c:281
const gchar * vtref_type(const vtref_t *r)
Get the type of a reference.
Definition nvti.c:118
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition nvti.c:2219
int nvti_put_summary(nvti_t *n, gchar *summary)
Set the summary of a NVT, using the given memory.
Definition nvti.c:1333
const gchar * vtseverity_value(const vtseverity_t *s)
Get the value of a severity.
Definition nvti.c:253
int nvti_set_insight(nvti_t *n, const gchar *insight)
Set the insight text of a NVT.
Definition nvti.c:1353
vtref_t * vtref_new(const gchar *type, const gchar *ref_id, const gchar *ref_text)
Create a new vtref structure filled with the given values.
Definition nvti.c:78
gchar * nvti_cvss_base(const nvti_t *n)
Get the CVSS base.
Definition nvti.c:1057
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition nvti.c:1735
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:463
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition nvti.c:522
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1573
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition nvti.c:542
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1273
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2289
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition nvti.c:1169
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition nvti.c:2091
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition nvti.c:734
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition nvti.c:706
vtseverity_t * nvti_vtseverity(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:894
vtseverity_t * vtseverity_new(const gchar *type, const gchar *origin, int date, double score, const gchar *value)
Create a new vtseverity structure filled with the given values.
Definition nvti.c:182
gchar * nvti_solution(const nvti_t *n)
Get the solution.
Definition nvti.c:963
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1492
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1473
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition nvti.c:1085
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition nvti.c:692
int nvti_add_refs(nvti_t *n, const gchar *type, const gchar *ref_ids, const gchar *ref_text)
Add many new vtref from a comma-separated list.
Definition nvti.c:2007
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition nvti.c:748
void vtref_free(vtref_t *ref)
Free memory of a vtref structure.
Definition nvti.c:98
const gchar * vtseverity_origin(const vtseverity_t *s)
Get the origin of a severity.
Definition nvti.c:239
guint nvti_vtref_len(const nvti_t *n)
Get the number of references of the NVT.
Definition nvti.c:761
int nvti_set_required_udp_ports(nvti_t *n, const gchar *required_udp_ports)
Set the required udp ports of a NVT.
Definition nvti.c:1831
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition nvti.c:1099
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition nvti.c:584
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1942
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition nvti.c:556
static time_t parse_nvt_timestamp(const gchar *str_time)
Try convert an NVT tag time string into epoch time or return 0 upon parse errors.
Definition nvti.c:297
int nvti_set_affected(nvti_t *n, const gchar *affected)
Set the affected text of a NVT.
Definition nvti.c:1393
int vtseverity_date(const vtseverity_t *s)
Get the date of a severity.
Definition nvti.c:267
gchar * nvti_solution_type(const nvti_t *n)
Get the solution type.
Definition nvti.c:977
const gchar * vtref_id(const vtref_t *r)
Get the id of a reference.
Definition nvti.c:132
int nvti_add_tag(nvti_t *n, const gchar *name, const gchar *value)
Add a tag to the NVT tags. The tag names "severity_date", "last_modification" and "creation_date" are...
Definition nvti.c:1600
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2272
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition nvti.c:720
const gchar * vtref_text(const vtref_t *r)
Get the text of a reference.
Definition nvti.c:146
void nvtis_free(nvtis_t *nvtis)
Free a collection of NVT Infos.
Definition nvti.c:2259
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1854
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition nvti.c:1005
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1982
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:609
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition nvti.c:444
Protos and data structures for NVT Information data sets.
GHashTable nvtis_t
A collection of information records corresponding to NVTs.
Definition nvti.h:252
String utilities.
The structure of a information record that corresponds to a NVT.
Definition nvti.c:394
gchar * solution_method
The solution method.
Definition nvti.c:408
gchar * insight
The insight.
Definition nvti.c:399
gchar * affected
Affected systems.
Definition nvti.c:400
gchar * cvss_base
CVSS base score for this NVT.
Definition nvti.c:411
gchar * required_keys
List of required KB keys of this NVT.
Definition nvti.c:414
time_t creation_time
Time of creation, seconds since epoch.
Definition nvti.c:403
gchar * solution
The solution.
Definition nvti.c:406
gchar * impact
Impact of vulnerability.
Definition nvti.c:401
gchar * family
Family the NVT belongs to.
Definition nvti.c:431
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition nvti.c:416
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition nvti.c:419
GSList * refs
Collection of VT references.
Definition nvti.c:425
gchar * tag
List of tags attached to this NVT.
Definition nvti.c:410
gchar * oid
Object ID.
Definition nvti.c:395
gchar * name
The name.
Definition nvti.c:396
gint category
The category, this NVT belongs to.
Definition nvti.c:430
gchar * qod_type
Quality of detection type.
Definition nvti.c:422
GSList * severities
Collection of VT severities.
Definition nvti.c:426
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition nvti.c:415
gchar * detection
Detection description.
Definition nvti.c:421
gchar * solution_type
The solution type.
Definition nvti.c:407
gchar * dependencies
List of dependencies of this NVT.
Definition nvti.c:413
gchar * qod
Quality of detection.
Definition nvti.c:423
time_t modification_time
Time of last change, sec. since epoch.
Definition nvti.c:404
gchar * required_ports
List of required ports of this NVT.
Definition nvti.c:417
gchar * summary
The summary.
Definition nvti.c:398
GSList * prefs
Collection of NVT preferences.
Definition nvti.c:427
The structure for a preference of a NVT.
Definition nvti.c:478
gchar * type
Preference type.
Definition nvti.c:480
gchar * dflt
Default value of the preference.
Definition nvti.c:482
int id
Preference ID.
Definition nvti.c:479
gchar * name
Name of the preference.
Definition nvti.c:481
The structure for a cross reference of a VT.
Definition nvti.c:58
gchar * type
Reference type ("cve", "bid", ...)
Definition nvti.c:59
gchar * ref_id
Actual reference ID ("CVE-2018-1234", etc)
Definition nvti.c:60
gchar * ref_text
Optional additional text.
Definition nvti.c:61
The structure for a severity of a VT.
Definition nvti.c:159
gchar * origin
Definition nvti.c:161
int date
Timestamp in seconds since epoch, defaults to VT creation date.
Definition nvti.c:163
double score
The score derived from the value in range [0.0-10.0].
Definition nvti.c:164
gchar * value
The value which corresponds to the type.
Definition nvti.c:165
gchar * type
Severity type ("cvss_base_v2", ...)
Definition nvti.c:160