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, "%Y-%m-%d %T +%H%M", &tm) == NULL && strptime ((char *) str_time, "%Y-%m-%d %T -%H%M", &tm) == NULL)
320 {
321 memset (&tm, 0, sizeof (struct tm));
322 if (strptime ((char *) str_time, "$Date: %Y-%m-%d %T +%H%M", &tm) == NULL && strptime ((char *) str_time, "$Date: %Y-%m-%d %T -%H%M", &tm) == NULL)
323 {
324 memset (&tm, 0, sizeof (struct tm));
325 if (strptime ((char *) str_time, "%a %b %d %T %Y +%H%M", &tm) == NULL && strptime ((char *) str_time, "%a %b %d %T %Y -%H%M", &tm) == NULL)
326 {
327 memset (&tm, 0, sizeof (struct tm));
328 if (strptime ((char *) str_time, "$Date: %a, %d %b %Y %T +%H%M", &tm) == NULL && strptime ((char *) str_time, "$Date: %a, %d %b %Y %T -%H%M", &tm) == NULL)
329 {
330 memset (&tm, 0, sizeof (struct tm));
331 if (strptime ((char *) str_time, "$Date: %a %b %d %T %Y +%H%M", &tm) == NULL && strptime ((char *) str_time, "$Date: %a %b %d %T %Y -%H%M", &tm) == NULL)
332 {
333 g_warning ("%s: Failed to parse time: %s", __func__,
334 str_time);
335 return 0;
336 }
337 }
338 }
339 }
340 }
341 epoch_time = mktime (&tm);
342 if (epoch_time == -1)
343 {
344 g_warning ("%s: Failed to make time: %s", __func__, str_time);
345 return 0;
346 }
347
348 /* Get the timezone offset from the str_time. */
349
350 if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
351 != 1)
352 && (sscanf ((char *) str_time, "$Date: %*u-%*u-%*u %*u:%*u:%*u %d%*[^]]",
353 &offset)
354 != 1)
355 && (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
356 &offset)
357 != 1)
358 && (sscanf ((char *) str_time,
359 "$Date: %*s %*s %*s %*u %*u:%*u:%*u %d%*[^]]", &offset)
360 != 1)
361 && (sscanf ((char *) str_time,
362 "$Date: %*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]", &offset)
363 != 1))
364 {
365 g_warning ("%s: Failed to parse timezone offset: %s", __func__, str_time);
366 return 0;
367 }
368
369 /* Use the offset to convert to UTC. */
370
371 if (offset < 0)
372 {
373 epoch_time += ((-offset) / 100) * 60 * 60;
374 epoch_time += ((-offset) % 100) * 60;
375 }
376 else if (offset > 0)
377 {
378 epoch_time -= (offset / 100) * 60 * 60;
379 epoch_time -= (offset % 100) * 60;
380 }
381
382 return epoch_time;
383}
384
385/* VT Information */
386
390typedef struct nvti
391{
392 gchar *oid;
393 gchar *name;
395 gchar *summary;
396 gchar *insight;
397 gchar *affected;
398 gchar *impact;
403 gchar *solution;
407 gchar *tag;
408 gchar *cvss_base;
415 gchar
418 gchar *detection;
419 gchar *qod_type;
420 gchar *qod;
422 GSList *refs;
423 GSList *severities;
424 GSList *prefs;
426 // The following are not settled yet.
427 gint category;
428 gchar *family;
430
440int
442{
443 if (!vt)
444 return -1;
445
446 vt->refs = g_slist_append (vt->refs, ref);
447 return 0;
448}
449
459int
461{
462 if (!vt)
463 return -1;
464
465 vt->severities = g_slist_append (vt->severities, s);
466 return 0;
467}
468
469/* VT preferences */
470
474typedef struct nvtpref
475{
476 int id;
477 gchar *type;
478 gchar *name;
479 gchar *dflt;
481
497nvtpref_t *
498nvtpref_new (int id, const gchar *name, const gchar *type, const gchar *dflt)
499{
500 nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
501
502 np->id = id;
503 if (name)
504 np->name = g_strdup (name);
505 if (type)
506 np->type = g_strdup (type);
507 if (dflt)
508 np->dflt = g_strdup (dflt);
509
510 return np;
511}
512
518void
520{
521 if (!np)
522 return;
523
524 g_free (np->name);
525 g_free (np->type);
526 g_free (np->dflt);
527 g_free (np);
528}
529
538int
540{
541 return np ? np->id : -1;
542}
543
552gchar *
554{
555 return np ? np->name : NULL;
556}
557
566gchar *
568{
569 return np ? np->type : NULL;
570}
571
580gchar *
582{
583 return np ? np->dflt : NULL;
584}
585
594nvti_t *
596{
597 return (nvti_t *) g_malloc0 (sizeof (nvti_t));
598}
599
605void
607{
608 if (!n)
609 return;
610
611 g_free (n->oid);
612 g_free (n->name);
613 g_free (n->summary);
614 g_free (n->insight);
615 g_free (n->affected);
616 g_free (n->impact);
617 g_free (n->solution);
618 g_free (n->solution_type);
619 g_free (n->solution_method);
620 g_free (n->tag);
621 g_free (n->cvss_base);
622 g_free (n->dependencies);
623 g_free (n->required_keys);
624 g_free (n->mandatory_keys);
625 g_free (n->excluded_keys);
626 g_free (n->required_ports);
627 g_free (n->required_udp_ports);
628 g_free (n->detection);
629 g_free (n->qod_type);
630 g_free (n->qod);
631 g_free (n->family);
632 g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
633 g_slist_free_full (n->severities, (void (*) (void *)) vtseverity_free);
634 g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
635 g_free (n);
636}
637
646gchar *
647nvti_oid (const nvti_t *n)
648{
649 return n ? n->oid : NULL;
650}
651
660gchar *
662{
663 return n ? n->name : NULL;
664}
665
674gchar *
676{
677 return n ? n->summary : NULL;
678}
679
688gchar *
690{
691 return n ? n->insight : NULL;
692}
693
702gchar *
704{
705 return n ? n->affected : NULL;
706}
707
716gchar *
718{
719 return n ? n->impact : NULL;
720}
721
730time_t
732{
733 return n ? n->creation_time : 0;
734}
735
744time_t
746{
747 return n ? n->modification_time : 0;
748}
749
757guint
759{
760 return n ? g_slist_length (n->refs) : 0;
761}
762
772vtref_t *
773nvti_vtref (const nvti_t *n, guint p)
774{
775 return n ? g_slist_nth_data (n->refs, p) : NULL;
776}
777
801gchar *
802nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
803 guint use_types)
804{
805 gchar *refs, *refs2, **exclude_item;
806 vtref_t *ref;
807 guint i, exclude;
808 gchar **exclude_split;
809
810 if (!n)
811 return NULL;
812
813 refs = NULL;
814 refs2 = NULL;
815 exclude = 0;
816
817 if (exclude_types && exclude_types[0])
818 exclude_split = g_strsplit (exclude_types, ",", 0);
819 else
820 exclude_split = NULL;
821
822 for (i = 0; i < g_slist_length (n->refs); i++)
823 {
824 ref = g_slist_nth_data (n->refs, i);
825 if (type && strcasecmp (ref->type, type) != 0)
826 continue;
827
828 if (exclude_split)
829 {
830 exclude = 0;
831 for (exclude_item = exclude_split; *exclude_item; exclude_item++)
832 {
833 if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
834 {
835 exclude = 1;
836 break;
837 }
838 }
839 }
840
841 if (!exclude)
842 {
843 if (use_types)
844 {
845 if (refs)
846 refs2 =
847 g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
848 else
849 refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
850 }
851 else
852 {
853 if (refs)
854 refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
855 else
856 refs2 = g_strdup_printf ("%s", ref->ref_id);
857 }
858 g_free (refs);
859 refs = refs2;
860 }
861 }
862
863 g_strfreev (exclude_split);
864
865 return refs;
866}
867
875guint
877{
878 return n ? g_slist_length (n->severities) : 0;
879}
880
891nvti_vtseverity (const nvti_t *n, guint p)
892{
893 return n ? g_slist_nth_data (n->severities, p) : NULL;
894}
895
903double
905{
906 unsigned int i;
907 double score = -1.0;
908
909 for (i = 0; i < nvti_vtseverities_len (n); i++)
910 {
911 vtseverity_t *severity;
912
913 severity = nvti_vtseverity (n, i);
914 if (vtseverity_score (severity) > score)
915 score = vtseverity_score (severity);
916 }
917
918 return score;
919}
920
935gchar *
937{
938 gchar *vector;
939
940 /* Currently, only one severity_vector can be stored as tag.
941 * Therfore we just check this one. */
942 vector = nvti_get_tag (n, "severity_vector");
943 if (vector)
944 return vector;
945
946 vector = nvti_get_tag (n, "cvss_base_vector");
947
948 return vector;
949}
950
959gchar *
961{
962 return n ? n->solution : NULL;
963}
964
973gchar *
975{
976 return n ? n->solution_type : NULL;
977}
978
987gchar *
989{
990 return n ? n->solution_method : NULL;
991}
992
1001gchar *
1003{
1004 return n ? n->tag : NULL;
1005}
1006
1017gchar *
1018nvti_get_tag (const nvti_t *n, const gchar *name)
1019{
1020 gchar **split, **point;
1021
1022 if (!n || n->tag == NULL || !name)
1023 return NULL;
1024
1025 split = g_strsplit (n->tag, "|", 0);
1026 point = split;
1027
1028 while (*point)
1029 {
1030 if ((strlen (*point) > strlen (name))
1031 && (strncmp (*point, name, strlen (name)) == 0)
1032 && ((*point)[strlen (name)] == '='))
1033 {
1034 gchar *ret;
1035 ret = g_strdup (*point + strlen (name) + 1);
1036 g_strfreev (split);
1037 return ret;
1038 }
1039 point++;
1040 }
1041 g_strfreev (split);
1042 return NULL;
1043}
1044
1053gchar *
1055{
1056 return n ? n->cvss_base : NULL;
1057}
1058
1067gchar *
1069{
1070 return n ? n->dependencies : NULL;
1071}
1072
1081gchar *
1083{
1084 return n ? n->required_keys : NULL;
1085}
1086
1095gchar *
1097{
1098 return n ? n->mandatory_keys : NULL;
1099}
1100
1109gchar *
1111{
1112 return n ? n->excluded_keys : NULL;
1113}
1114
1123gchar *
1125{
1126 return n ? n->required_ports : NULL;
1127}
1128
1137gchar *
1139{
1140 return n ? n->required_udp_ports : NULL;
1141}
1142
1151gchar *
1153{
1154 return n ? n->detection : NULL;
1155}
1156
1165gchar *
1167{
1168 return n ? n->qod_type : NULL;
1169}
1170
1179gchar *
1181{
1182 return n ? n->qod : NULL;
1183}
1184
1193gchar *
1195{
1196 return n ? n->family : NULL;
1197}
1198
1206guint
1208{
1209 return n ? g_slist_length (n->prefs) : 0;
1210}
1211
1221const nvtpref_t *
1222nvti_pref (const nvti_t *n, guint p)
1223{
1224 return n ? g_slist_nth_data (n->prefs, p) : NULL;
1225}
1226
1234gint
1236{
1237 return n ? n->category : -1;
1238}
1239
1249int
1250nvti_set_oid (nvti_t *n, const gchar *oid)
1251{
1252 if (!n)
1253 return -1;
1254
1255 g_free (n->oid);
1256 n->oid = g_strdup (oid);
1257 return 0;
1258}
1259
1269int
1270nvti_set_name (nvti_t *n, const gchar *name)
1271{
1272 if (!n)
1273 return -1;
1274
1275 g_free (n->name);
1276 n->name = g_strdup (name);
1277 return 0;
1278}
1279
1289int
1290nvti_put_name (nvti_t *n, gchar *name)
1291{
1292 if (!n)
1293 return -1;
1294
1295 g_free (n->name);
1296 n->name = name;
1297 return 0;
1298}
1299
1309int
1310nvti_set_summary (nvti_t *n, const gchar *summary)
1311{
1312 if (!n)
1313 return -1;
1314
1315 g_free (n->summary);
1316 n->summary = g_strdup (summary);
1317 return 0;
1318}
1319
1329int
1330nvti_put_summary (nvti_t *n, gchar *summary)
1331{
1332 if (!n)
1333 return -1;
1334
1335 g_free (n->summary);
1336 n->summary = summary;
1337 return 0;
1338}
1339
1349int
1350nvti_set_insight (nvti_t *n, const gchar *insight)
1351{
1352 if (!n)
1353 return -1;
1354
1355 g_free (n->insight);
1356 n->insight = g_strdup (insight);
1357 return 0;
1358}
1359
1369int
1370nvti_put_insight (nvti_t *n, gchar *insight)
1371{
1372 if (!n)
1373 return -1;
1374
1375 g_free (n->insight);
1376 n->insight = insight;
1377 return 0;
1378}
1379
1389int
1390nvti_set_affected (nvti_t *n, const gchar *affected)
1391{
1392 if (!n)
1393 return -1;
1394
1395 g_free (n->affected);
1396 n->affected = g_strdup (affected);
1397 return 0;
1398}
1399
1409int
1410nvti_put_affected (nvti_t *n, gchar *affected)
1411{
1412 if (!n)
1413 return -1;
1414
1415 g_free (n->affected);
1416 n->affected = affected;
1417 return 0;
1418}
1419
1429int
1430nvti_set_impact (nvti_t *n, const gchar *impact)
1431{
1432 if (!n)
1433 return -1;
1434
1435 g_free (n->impact);
1436 n->impact = g_strdup (impact);
1437 return 0;
1438}
1439
1449int
1450nvti_put_impact (nvti_t *n, gchar *impact)
1451{
1452 if (!n)
1453 return -1;
1454
1455 g_free (n->impact);
1456 n->impact = impact;
1457 return 0;
1458}
1459
1469int
1470nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1471{
1472 if (!n)
1473 return -1;
1474
1475 n->creation_time = creation_time;
1476 return 0;
1477}
1478
1488int
1489nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1490{
1491 if (!n)
1492 return -1;
1493
1494 n->modification_time = modification_time;
1495 return 0;
1496}
1497
1507int
1508nvti_set_solution (nvti_t *n, const gchar *solution)
1509{
1510 if (!n)
1511 return -1;
1512
1513 g_free (n->solution);
1514 n->solution = g_strdup (solution);
1515 return 0;
1516}
1517
1527int
1528nvti_put_solution (nvti_t *n, gchar *solution)
1529{
1530 if (!n)
1531 return -1;
1532
1533 g_free (n->solution);
1534 n->solution = solution;
1535 return 0;
1536}
1537
1548int
1549nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1550{
1551 if (!n)
1552 return -1;
1553
1554 g_free (n->solution_type);
1555 n->solution_type = g_strdup (solution_type);
1556 return 0;
1557}
1558
1569int
1570nvti_set_solution_method (nvti_t *n, const gchar *solution_method)
1571{
1572 if (!n)
1573 return -1;
1574
1575 g_free (n->solution_method);
1576 n->solution_method = g_strdup (solution_method);
1577 return 0;
1578}
1579
1596int
1597nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1598{
1599 gchar *newvalue = NULL;
1600
1601 if (!n)
1602 return -1;
1603
1604 if (!name || !name[0])
1605 return -2;
1606
1607 if (!value || !value[0])
1608 return -3;
1609
1610 if (!strcmp (name, "last_modification"))
1611 {
1613 newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1614 }
1615 else if (!strcmp (name, "creation_date"))
1616 {
1618 newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1619 }
1620 else if (!strcmp (name, "severity_date"))
1621 newvalue = g_strdup_printf ("%i", (int) parse_nvt_timestamp (value));
1622 else if (!strcmp (name, "cvss_base"))
1623 {
1624 /* Ignore this tag because it is not being used.
1625 * It is redundant with the tag cvss_base_vector from which
1626 * it is computed.
1627 * Once GOS 6 and GVM 11 are retired, all set_tag commands
1628 * in the NASL scripts can be removed that set "cvss_base".
1629 * Once this happened this exception can be removed from the code.
1630 */
1631 return 0;
1632 }
1633
1634 if (n->tag)
1635 {
1636 gchar *newtag;
1637
1638 newtag =
1639 g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1640 g_free (n->tag);
1641 n->tag = newtag;
1642 }
1643 else
1644 n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1645
1646 g_free (newvalue);
1647
1648 return 0;
1649}
1650
1660int
1661nvti_set_tag (nvti_t *n, const gchar *tag)
1662{
1663 if (!n)
1664 return -1;
1665
1666 g_free (n->tag);
1667 if (tag && tag[0])
1668 n->tag = g_strdup (tag);
1669 else
1670 n->tag = NULL;
1671 return 0;
1672}
1673
1683int
1684nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1685{
1686 if (!n)
1687 return -1;
1688
1689 g_free (n->cvss_base);
1690 if (cvss_base && cvss_base[0])
1691 n->cvss_base = g_strdup (cvss_base);
1692 else
1693 n->cvss_base = NULL;
1694 return 0;
1695}
1696
1707int
1708nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1709{
1710 if (!n)
1711 return -1;
1712
1713 g_free (n->dependencies);
1714 if (dependencies && dependencies[0])
1715 n->dependencies = g_strdup (dependencies);
1716 else
1717 n->dependencies = NULL;
1718 return 0;
1719}
1720
1731int
1732nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1733{
1734 if (!n)
1735 return -1;
1736
1737 g_free (n->required_keys);
1738 if (required_keys && required_keys[0])
1739 n->required_keys = g_strdup (required_keys);
1740 else
1741 n->required_keys = NULL;
1742 return 0;
1743}
1744
1755int
1756nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1757{
1758 if (!n)
1759 return -1;
1760
1761 g_free (n->mandatory_keys);
1762 if (mandatory_keys && mandatory_keys[0])
1763 n->mandatory_keys = g_strdup (mandatory_keys);
1764 else
1765 n->mandatory_keys = NULL;
1766 return 0;
1767}
1768
1779int
1780nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1781{
1782 if (!n)
1783 return -1;
1784
1785 g_free (n->excluded_keys);
1786 if (excluded_keys && excluded_keys[0])
1787 n->excluded_keys = g_strdup (excluded_keys);
1788 else
1789 n->excluded_keys = NULL;
1790 return 0;
1791}
1792
1803int
1804nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1805{
1806 if (!n)
1807 return -1;
1808
1809 g_free (n->required_ports);
1810 if (required_ports && required_ports[0])
1811 n->required_ports = g_strdup (required_ports);
1812 else
1813 n->required_ports = NULL;
1814 return 0;
1815}
1816
1827int
1828nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1829{
1830 if (!n)
1831 return -1;
1832
1833 g_free (n->required_udp_ports);
1834 if (required_udp_ports && required_udp_ports[0])
1835 n->required_udp_ports = g_strdup (required_udp_ports);
1836 else
1837 n->required_udp_ports = NULL;
1838 return 0;
1839}
1840
1850int
1851nvti_set_detection (nvti_t *n, const gchar *detection)
1852{
1853 if (!n)
1854 return -1;
1855
1856 g_free (n->detection);
1857 n->detection = g_strdup (detection);
1858 return 0;
1859}
1860
1870int
1871nvti_put_detection (nvti_t *n, gchar *detection)
1872{
1873 if (!n)
1874 return -1;
1875
1876 g_free (n->detection);
1877 n->detection = detection;
1878 return 0;
1879}
1880
1891int
1892nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1893{
1894 if (!n)
1895 return -1;
1896
1897 g_free (n->qod_type);
1898 if (qod_type && qod_type[0])
1899 n->qod_type = g_strdup (qod_type);
1900 else
1901 n->qod_type = NULL;
1902 return 0;
1903}
1904
1915int
1916nvti_set_qod (nvti_t *n, const gchar *qod)
1917{
1918 if (!n)
1919 return -1;
1920
1921 g_free (n->qod);
1922 if (qod && qod[0])
1923 n->qod = g_strdup (qod);
1924 else
1925 n->qod = NULL;
1926 return 0;
1927}
1928
1938int
1939nvti_set_family (nvti_t *n, const gchar *family)
1940{
1941 if (!n)
1942 return -1;
1943
1944 g_free (n->family);
1945 n->family = g_strdup (family);
1946 return 0;
1947}
1948
1958int
1959nvti_put_family (nvti_t *n, gchar *family)
1960{
1961 if (!n)
1962 return -1;
1963
1964 g_free (n->family);
1965 n->family = family;
1966 return 0;
1967}
1968
1978int
1979nvti_set_category (nvti_t *n, const gint category)
1980{
1981 if (!n)
1982 return -1;
1983
1984 n->category = category;
1985 return 0;
1986}
1987
2003int
2004nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
2005 const gchar *ref_text)
2006{
2007 gchar **split, **item;
2008
2009 if (!n)
2010 return 1;
2011
2012 if (!ref_ids)
2013 return 2;
2014
2015 split = g_strsplit (ref_ids, ",", 0);
2016
2017 for (item = split; *item; item++)
2018 {
2019 gchar *id;
2020
2021 id = *item;
2022 g_strstrip (id);
2023
2024 if (strcmp (id, "") == 0)
2025 continue;
2026
2027 if (type)
2028 {
2029 nvti_add_vtref (n, vtref_new (type, id, ref_text));
2030 }
2031 else
2032 {
2033 gchar **split2;
2034
2035 split2 = g_strsplit (id, ":", 2);
2036 if (split2[0] && split2[1])
2037 nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
2038 g_strfreev (split2);
2039 }
2040 }
2041 g_strfreev (split);
2042
2043 return 0;
2044}
2045
2055int
2056nvti_add_required_keys (nvti_t *n, const gchar *key)
2057{
2058 gchar *old;
2059
2060 if (!n)
2061 return 1;
2062 if (!key)
2063 return 2;
2064
2065 old = n->required_keys;
2066
2067 if (old)
2068 {
2069 n->required_keys = g_strdup_printf ("%s, %s", old, key);
2070 g_free (old);
2071 }
2072 else
2073 n->required_keys = g_strdup (key);
2074
2075 return 0;
2076}
2077
2087int
2088nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
2089{
2090 gchar *old;
2091
2092 if (!n)
2093 return 1;
2094 if (!key)
2095 return 2;
2096
2097 old = n->mandatory_keys;
2098
2099 if (old)
2100 {
2101 n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
2102 g_free (old);
2103 }
2104 else
2105 n->mandatory_keys = g_strdup (key);
2106
2107 return 0;
2108}
2109
2119int
2120nvti_add_excluded_keys (nvti_t *n, const gchar *key)
2121{
2122 gchar *old;
2123
2124 if (!n)
2125 return 1;
2126 if (!key)
2127 return 2;
2128
2129 old = n->excluded_keys;
2130
2131 if (old)
2132 {
2133 n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
2134 g_free (old);
2135 }
2136 else
2137 n->excluded_keys = g_strdup (key);
2138
2139 return 0;
2140}
2141
2151int
2152nvti_add_required_ports (nvti_t *n, const gchar *port)
2153{
2154 gchar *old;
2155
2156 if (!n)
2157 return 1;
2158 if (!port)
2159 return 2;
2160
2161 old = n->required_ports;
2162
2163 if (old)
2164 {
2165 n->required_ports = g_strdup_printf ("%s, %s", old, port);
2166 g_free (old);
2167 }
2168 else
2169 n->required_ports = g_strdup (port);
2170
2171 return 0;
2172}
2173
2183int
2185{
2186 gchar *old;
2187
2188 if (!n)
2189 return 1;
2190 if (!port)
2191 return 2;
2192
2193 old = n->required_udp_ports;
2194
2195 if (old)
2196 {
2197 n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
2198 g_free (old);
2199 }
2200 else
2201 n->required_udp_ports = g_strdup (port);
2202
2203 return 0;
2204}
2205
2215int
2217{
2218 if (!n)
2219 return -1;
2220
2221 n->prefs = g_slist_append (n->prefs, np);
2222 return 0;
2223}
2224
2225/* Collections of nvtis. */
2226
2232static void
2234{
2235 nvti_free ((nvti_t *) nvti);
2236}
2237
2243nvtis_t *
2245{
2246 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2248}
2249
2255void
2257{
2258 if (nvtis)
2259 g_hash_table_destroy (nvtis);
2260}
2261
2268void
2270{
2271 if (nvti)
2272 g_hash_table_insert (
2273 nvtis, (gpointer) (nvti_oid (nvti) ? g_strdup (nvti_oid (nvti)) : NULL),
2274 (gpointer) nvti);
2275}
2276
2285nvti_t *
2286nvtis_lookup (nvtis_t *nvtis, const char *oid)
2287{
2288 return g_hash_table_lookup (nvtis, oid);
2289}
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:773
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:595
int nvti_put_impact(nvti_t *n, gchar *impact)
Set the impact text of a NVT, using the given memory.
Definition nvti.c:1450
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:1068
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition nvti.c:2056
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition nvti.c:1207
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1916
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition nvti.c:1152
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition nvti.c:1138
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1508
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:1018
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition nvti.c:675
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1549
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition nvti.c:1124
int nvti_put_insight(nvti_t *n, gchar *insight)
Set the insight text of a NVT, using the given memory.
Definition nvti.c:1370
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:802
double nvti_severity_score(const nvti_t *n)
Get the maximum severity score.
Definition nvti.c:904
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition nvti.c:567
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition nvti.c:1780
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:1892
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition nvti.c:2120
int nvti_put_detection(nvti_t *n, gchar *detection)
Set the detection text of a NVT, using the given memory.
Definition nvti.c:1871
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2244
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition nvti.c:1708
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition nvti.c:1804
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1250
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition nvti.c:2184
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1661
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1684
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition nvti.c:1756
int nvti_put_name(nvti_t *n, gchar *name)
Set the name of a NVT, using the given memory.
Definition nvti.c:1290
int nvti_put_family(nvti_t *n, gchar *family)
Set the family of a NVT, using the given memory.
Definition nvti.c:1959
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition nvti.c:1235
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1430
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:2152
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:1194
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:936
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:2233
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:988
guint nvti_vtseverities_len(const nvti_t *n)
Get the number of severities of the NVT.
Definition nvti.c:876
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition nvti.c:1222
gchar * nvti_qod(const nvti_t *n)
Get the QoD.
Definition nvti.c:1180
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition nvti.c:1110
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1310
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:661
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:647
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:498
int nvti_put_affected(nvti_t *n, gchar *affected)
Set the affected text of a NVT, using the given memory.
Definition nvti.c:1410
int nvti_put_solution(nvti_t *n, gchar *solution)
Set the solution of a NVT, using the given memory.
Definition nvti.c:1528
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:2216
int nvti_put_summary(nvti_t *n, gchar *summary)
Set the summary of a NVT, using the given memory.
Definition nvti.c:1330
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:1350
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:1054
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition nvti.c:1732
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:460
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition nvti.c:519
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1570
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition nvti.c:539
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1270
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2286
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition nvti.c:1166
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition nvti.c:2088
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition nvti.c:731
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition nvti.c:703
vtseverity_t * nvti_vtseverity(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:891
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:960
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1489
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1470
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition nvti.c:1082
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition nvti.c:689
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:2004
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition nvti.c:745
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:758
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:1828
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition nvti.c:1096
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition nvti.c:581
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1939
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition nvti.c:553
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:1390
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:974
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:1597
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2269
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition nvti.c:717
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:2256
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1851
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition nvti.c:1002
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1979
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:606
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition nvti.c:441
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:391
gchar * solution_method
The solution method.
Definition nvti.c:405
gchar * insight
The insight.
Definition nvti.c:396
gchar * affected
Affected systems.
Definition nvti.c:397
gchar * cvss_base
CVSS base score for this NVT.
Definition nvti.c:408
gchar * required_keys
List of required KB keys of this NVT.
Definition nvti.c:411
time_t creation_time
Time of creation, seconds since epoch.
Definition nvti.c:400
gchar * solution
The solution.
Definition nvti.c:403
gchar * impact
Impact of vulnerability.
Definition nvti.c:398
gchar * family
Family the NVT belongs to.
Definition nvti.c:428
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition nvti.c:413
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition nvti.c:416
GSList * refs
Collection of VT references.
Definition nvti.c:422
gchar * tag
List of tags attached to this NVT.
Definition nvti.c:407
gchar * oid
Object ID.
Definition nvti.c:392
gchar * name
The name.
Definition nvti.c:393
gint category
The category, this NVT belongs to.
Definition nvti.c:427
gchar * qod_type
Quality of detection type.
Definition nvti.c:419
GSList * severities
Collection of VT severities.
Definition nvti.c:423
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition nvti.c:412
gchar * detection
Detection description.
Definition nvti.c:418
gchar * solution_type
The solution type.
Definition nvti.c:404
gchar * dependencies
List of dependencies of this NVT.
Definition nvti.c:410
gchar * qod
Quality of detection.
Definition nvti.c:420
time_t modification_time
Time of last change, sec. since epoch.
Definition nvti.c:401
gchar * required_ports
List of required ports of this NVT.
Definition nvti.c:414
gchar * summary
The summary.
Definition nvti.c:395
GSList * prefs
Collection of NVT preferences.
Definition nvti.c:424
The structure for a preference of a NVT.
Definition nvti.c:475
gchar * type
Preference type.
Definition nvti.c:477
gchar * dflt
Default value of the preference.
Definition nvti.c:479
int id
Preference ID.
Definition nvti.c:476
gchar * name
Name of the preference.
Definition nvti.c:478
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