Greenbone Vulnerability Management Libraries 22.18.1
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
21
32
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
76vtref_t *
77vtref_new (const gchar *type, const gchar *ref_id, const gchar *ref_text)
78{
79 vtref_t *ref = g_malloc0 (sizeof (vtref_t));
80
81 if (type)
82 ref->type = g_strdup (type);
83 if (ref_id)
84 ref->ref_id = g_strdup (ref_id);
85 if (ref_text)
86 ref->ref_text = g_strdup (ref_text);
87
88 return ref;
89}
90
96void
98{
99 if (!ref)
100 return;
101
102 g_free (ref->type);
103 g_free (ref->ref_id);
104 g_free (ref->ref_text);
105 g_free (ref);
106}
107
116const gchar *
118{
119 return r ? r->type : NULL;
120}
121
130const gchar *
132{
133 return r ? r->ref_id : NULL;
134}
135
144const gchar *
146{
147 return r ? r->ref_text : NULL;
148}
149
150/* VT severities */
151
157typedef struct vtseverity
158{
159 gchar *type;
160 gchar *origin;
162 int date;
163 double score;
164 gchar *value;
166
180vtseverity_new (const gchar *type, const gchar *origin, int date, double score,
181 const gchar *value)
182{
183 vtseverity_t *s = g_malloc0 (sizeof (vtseverity_t));
184
185 if (type)
186 s->type = g_strdup (type);
187 if (origin)
188 s->origin = g_strdup (origin);
189 s->date = date;
190 s->score = score;
191 if (value)
192 s->value = g_strdup (value);
193
194 return s;
195}
196
202void
204{
205 if (!s)
206 return;
207
208 g_free (s->type);
209 g_free (s->origin);
210 g_free (s->value);
211 g_free (s);
212}
213
222const gchar *
224{
225 return s ? s->type : NULL;
226}
227
236const gchar *
238{
239 return s ? s->origin : NULL;
240}
241
250const gchar *
252{
253 return s ? s->value : NULL;
254}
255
264int
266{
267 return s->date;
268}
269
278double
280{
281 return s->score;
282}
283
284/* Support function for timestamps */
285
294static time_t
295parse_nvt_timestamp (const gchar *str_time)
296{
297 time_t epoch_time;
298 int offset;
299 struct tm tm;
300
301 if (strcmp ((char *) str_time, "") == 0)
302 return 0;
303
304 /* Parse the time. */
305
306 /* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
307 memset (&tm, 0, sizeof (struct tm));
308 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)
309 {
310 memset (&tm, 0, sizeof (struct tm));
311 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)
312 {
313 g_warning ("%s: Failed to parse time: %s", __func__, str_time);
314 return 0;
315 }
316 }
317 epoch_time = mktime (&tm);
318 if (epoch_time == -1)
319 {
320 g_warning ("%s: Failed to make time: %s", __func__, str_time);
321 return 0;
322 }
323
324 /* Get the timezone offset from the str_time. */
325
326 if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
327 != 1)
328 && (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
329 &offset)
330 != 1))
331 {
332 g_warning ("%s: Failed to parse timezone offset: %s", __func__, str_time);
333 return 0;
334 }
335
336 /* Use the offset to convert to UTC. */
337
338 if (offset < 0)
339 {
340 epoch_time += ((-offset) / 100) * 60 * 60;
341 epoch_time += ((-offset) % 100) * 60;
342 }
343 else if (offset > 0)
344 {
345 epoch_time -= (offset / 100) * 60 * 60;
346 epoch_time -= (offset % 100) * 60;
347 }
348
349 return epoch_time;
350}
351
352/* VT Information */
353
357typedef struct nvti
358{
359 gchar *oid;
360 gchar *name;
361
362 gchar *summary;
363 gchar *insight;
364 gchar *affected;
365 gchar *impact;
366
369
370 gchar *solution;
373
374 gchar *tag;
375 gchar *cvss_base;
376
382 gchar
384
385 gchar *detection;
386 gchar *qod_type;
387 gchar *qod;
388
389 GSList *refs;
390 GSList *severities;
391 GSList *prefs;
392
393 // The following are not settled yet.
394 gint category;
395 gchar *family;
397
407int
409{
410 if (!vt)
411 return -1;
412
413 vt->refs = g_slist_append (vt->refs, ref);
414 return 0;
415}
416
425int
427{
428 if (!vt)
429 return -1;
430
431 vt->severities = g_slist_append (vt->severities, s);
432 return 0;
433}
434
435/* VT preferences */
436
440typedef struct nvtpref
441{
442 int id;
443 gchar *type;
444 gchar *name;
445 gchar *dflt;
447
462nvtpref_t *
463nvtpref_new (int id, const gchar *name, const gchar *type, const gchar *dflt)
464{
465 nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
466
467 np->id = id;
468 if (name)
469 np->name = g_strdup (name);
470 if (type)
471 np->type = g_strdup (type);
472 if (dflt)
473 np->dflt = g_strdup (dflt);
474
475 return np;
476}
477
483void
485{
486 if (!np)
487 return;
488
489 g_free (np->name);
490 g_free (np->type);
491 g_free (np->dflt);
492 g_free (np);
493}
494
503int
505{
506 return np ? np->id : -1;
507}
508
517gchar *
519{
520 return np ? np->name : NULL;
521}
522
531gchar *
533{
534 return np ? np->type : NULL;
535}
536
545gchar *
547{
548 return np ? np->dflt : NULL;
549}
550
558nvti_t *
560{
561 return (nvti_t *) g_malloc0 (sizeof (nvti_t));
562}
563
569void
571{
572 if (!n)
573 return;
574
575 g_free (n->oid);
576 g_free (n->name);
577 g_free (n->summary);
578 g_free (n->insight);
579 g_free (n->affected);
580 g_free (n->impact);
581 g_free (n->solution);
582 g_free (n->solution_type);
583 g_free (n->solution_method);
584 g_free (n->tag);
585 g_free (n->cvss_base);
586 g_free (n->dependencies);
587 g_free (n->required_keys);
588 g_free (n->mandatory_keys);
589 g_free (n->excluded_keys);
590 g_free (n->required_ports);
591 g_free (n->required_udp_ports);
592 g_free (n->detection);
593 g_free (n->qod_type);
594 g_free (n->qod);
595 g_free (n->family);
596 g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
597 g_slist_free_full (n->severities, (void (*) (void *)) vtseverity_free);
598 g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
599 g_free (n);
600}
601
610gchar *
611nvti_oid (const nvti_t *n)
612{
613 return n ? n->oid : NULL;
614}
615
624gchar *
626{
627 return n ? n->name : NULL;
628}
629
638gchar *
640{
641 return n ? n->summary : NULL;
642}
643
652gchar *
654{
655 return n ? n->insight : NULL;
656}
657
666gchar *
668{
669 return n ? n->affected : NULL;
670}
671
680gchar *
682{
683 return n ? n->impact : NULL;
684}
685
694time_t
696{
697 return n ? n->creation_time : 0;
698}
699
708time_t
710{
711 return n ? n->modification_time : 0;
712}
713
721guint
723{
724 return n ? g_slist_length (n->refs) : 0;
725}
726
736vtref_t *
737nvti_vtref (const nvti_t *n, guint p)
738{
739 return n ? g_slist_nth_data (n->refs, p) : NULL;
740}
741
765gchar *
766nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
767 guint use_types)
768{
769 gchar *refs, *refs2, **exclude_item;
770 vtref_t *ref;
771 guint i, exclude;
772 gchar **exclude_split;
773
774 if (!n)
775 return NULL;
776
777 refs = NULL;
778 refs2 = NULL;
779 exclude = 0;
780
781 if (exclude_types && exclude_types[0])
782 exclude_split = g_strsplit (exclude_types, ",", 0);
783 else
784 exclude_split = NULL;
785
786 for (i = 0; i < g_slist_length (n->refs); i++)
787 {
788 ref = g_slist_nth_data (n->refs, i);
789 if (type && strcasecmp (ref->type, type) != 0)
790 continue;
791
792 if (exclude_split)
793 {
794 exclude = 0;
795 for (exclude_item = exclude_split; *exclude_item; exclude_item++)
796 {
797 if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
798 {
799 exclude = 1;
800 break;
801 }
802 }
803 }
804
805 if (!exclude)
806 {
807 if (use_types)
808 {
809 if (refs)
810 refs2 =
811 g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
812 else
813 refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
814 }
815 else
816 {
817 if (refs)
818 refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
819 else
820 refs2 = g_strdup_printf ("%s", ref->ref_id);
821 }
822 g_free (refs);
823 refs = refs2;
824 }
825 }
826
827 g_strfreev (exclude_split);
828
829 return refs;
830}
831
839guint
841{
842 return n ? g_slist_length (n->severities) : 0;
843}
844
855nvti_vtseverity (const nvti_t *n, guint p)
856{
857 return n ? g_slist_nth_data (n->severities, p) : NULL;
858}
859
867double
869{
870 unsigned int i;
871 double score = -1.0;
872
873 for (i = 0; i < nvti_vtseverities_len (n); i++)
874 {
875 vtseverity_t *severity;
876
877 severity = nvti_vtseverity (n, i);
878 if (vtseverity_score (severity) > score)
879 score = vtseverity_score (severity);
880 }
881
882 return score;
883}
884
899gchar *
901{
902 gchar *vector;
903
904 /* Currently, only one severity_vector can be stored as tag.
905 * Therfore we just check this one. */
906 vector = nvti_get_tag (n, "severity_vector");
907 if (vector)
908 return vector;
909
910 vector = nvti_get_tag (n, "cvss_base_vector");
911
912 return vector;
913}
914
923gchar *
925{
926 return n ? n->solution : NULL;
927}
928
937gchar *
939{
940 return n ? n->solution_type : NULL;
941}
942
951gchar *
953{
954 return n ? n->solution_method : NULL;
955}
956
965gchar *
966nvti_tag (const nvti_t *n)
967{
968 return n ? n->tag : NULL;
969}
970
981gchar *
982nvti_get_tag (const nvti_t *n, const gchar *name)
983{
984 gchar **split, **point;
985
986 if (!n || n->tag == NULL || !name)
987 return NULL;
988
989 split = g_strsplit (n->tag, "|", 0);
990 point = split;
991
992 while (*point)
993 {
994 if ((strlen (*point) > strlen (name))
995 && (strncmp (*point, name, strlen (name)) == 0)
996 && ((*point)[strlen (name)] == '='))
997 {
998 gchar *ret;
999 ret = g_strdup (*point + strlen (name) + 1);
1000 g_strfreev (split);
1001 return ret;
1002 }
1003 point++;
1004 }
1005 g_strfreev (split);
1006 return NULL;
1007}
1008
1017gchar *
1019{
1020 return n ? n->cvss_base : NULL;
1021}
1022
1031gchar *
1033{
1034 return n ? n->dependencies : NULL;
1035}
1036
1045gchar *
1047{
1048 return n ? n->required_keys : NULL;
1049}
1050
1059gchar *
1061{
1062 return n ? n->mandatory_keys : NULL;
1063}
1064
1073gchar *
1075{
1076 return n ? n->excluded_keys : NULL;
1077}
1078
1087gchar *
1089{
1090 return n ? n->required_ports : NULL;
1091}
1092
1101gchar *
1103{
1104 return n ? n->required_udp_ports : NULL;
1105}
1106
1115gchar *
1117{
1118 return n ? n->detection : NULL;
1119}
1120
1129gchar *
1131{
1132 return n ? n->qod_type : NULL;
1133}
1134
1143gchar *
1145{
1146 return n ? n->qod : NULL;
1147}
1148
1157gchar *
1159{
1160 return n ? n->family : NULL;
1161}
1162
1170guint
1172{
1173 return n ? g_slist_length (n->prefs) : 0;
1174}
1175
1185const nvtpref_t *
1186nvti_pref (const nvti_t *n, guint p)
1187{
1188 return n ? g_slist_nth_data (n->prefs, p) : NULL;
1189}
1190
1198gint
1200{
1201 return n ? n->category : -1;
1202}
1203
1213int
1214nvti_set_oid (nvti_t *n, const gchar *oid)
1215{
1216 if (!n)
1217 return -1;
1218
1219 g_free (n->oid);
1220 n->oid = g_strdup (oid);
1221 return 0;
1222}
1223
1233int
1234nvti_set_name (nvti_t *n, const gchar *name)
1235{
1236 if (!n)
1237 return -1;
1238
1239 g_free (n->name);
1240 n->name = g_strdup (name);
1241 return 0;
1242}
1243
1253int
1254nvti_put_name (nvti_t *n, gchar *name)
1255{
1256 if (!n)
1257 return -1;
1258
1259 g_free (n->name);
1260 n->name = name;
1261 return 0;
1262}
1263
1273int
1274nvti_set_summary (nvti_t *n, const gchar *summary)
1275{
1276 if (!n)
1277 return -1;
1278
1279 g_free (n->summary);
1280 n->summary = g_strdup (summary);
1281 return 0;
1282}
1283
1293int
1294nvti_put_summary (nvti_t *n, gchar *summary)
1295{
1296 if (!n)
1297 return -1;
1298
1299 g_free (n->summary);
1300 n->summary = summary;
1301 return 0;
1302}
1303
1313int
1314nvti_set_insight (nvti_t *n, const gchar *insight)
1315{
1316 if (!n)
1317 return -1;
1318
1319 g_free (n->insight);
1320 n->insight = g_strdup (insight);
1321 return 0;
1322}
1323
1333int
1334nvti_put_insight (nvti_t *n, gchar *insight)
1335{
1336 if (!n)
1337 return -1;
1338
1339 g_free (n->insight);
1340 n->insight = insight;
1341 return 0;
1342}
1343
1353int
1354nvti_set_affected (nvti_t *n, const gchar *affected)
1355{
1356 if (!n)
1357 return -1;
1358
1359 g_free (n->affected);
1360 n->affected = g_strdup (affected);
1361 return 0;
1362}
1363
1373int
1374nvti_put_affected (nvti_t *n, gchar *affected)
1375{
1376 if (!n)
1377 return -1;
1378
1379 g_free (n->affected);
1380 n->affected = affected;
1381 return 0;
1382}
1383
1393int
1394nvti_set_impact (nvti_t *n, const gchar *impact)
1395{
1396 if (!n)
1397 return -1;
1398
1399 g_free (n->impact);
1400 n->impact = g_strdup (impact);
1401 return 0;
1402}
1403
1413int
1414nvti_put_impact (nvti_t *n, gchar *impact)
1415{
1416 if (!n)
1417 return -1;
1418
1419 g_free (n->impact);
1420 n->impact = impact;
1421 return 0;
1422}
1423
1433int
1434nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1435{
1436 if (!n)
1437 return -1;
1438
1439 n->creation_time = creation_time;
1440 return 0;
1441}
1442
1452int
1453nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1454{
1455 if (!n)
1456 return -1;
1457
1458 n->modification_time = modification_time;
1459 return 0;
1460}
1461
1471int
1472nvti_set_solution (nvti_t *n, const gchar *solution)
1473{
1474 if (!n)
1475 return -1;
1476
1477 g_free (n->solution);
1478 n->solution = g_strdup (solution);
1479 return 0;
1480}
1481
1491int
1492nvti_put_solution (nvti_t *n, gchar *solution)
1493{
1494 if (!n)
1495 return -1;
1496
1497 g_free (n->solution);
1498 n->solution = solution;
1499 return 0;
1500}
1501
1512int
1513nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1514{
1515 if (!n)
1516 return -1;
1517
1518 g_free (n->solution_type);
1519 n->solution_type = g_strdup (solution_type);
1520 return 0;
1521}
1522
1533int
1534nvti_set_solution_method (nvti_t *n, const gchar *solution_method)
1535{
1536 if (!n)
1537 return -1;
1538
1539 g_free (n->solution_method);
1540 n->solution_method = g_strdup (solution_method);
1541 return 0;
1542}
1543
1560int
1561nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1562{
1563 gchar *newvalue = NULL;
1564
1565 if (!n)
1566 return -1;
1567
1568 if (!name || !name[0])
1569 return -2;
1570
1571 if (!value || !value[0])
1572 return -3;
1573
1574 if (!strcmp (name, "last_modification"))
1575 {
1577 newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1578 }
1579 else if (!strcmp (name, "creation_date"))
1580 {
1582 newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1583 }
1584 else if (!strcmp (name, "severity_date"))
1585 newvalue = g_strdup_printf ("%i", (int) parse_nvt_timestamp (value));
1586 else if (!strcmp (name, "cvss_base"))
1587 {
1588 /* Ignore this tag because it is not being used.
1589 * It is redundant with the tag cvss_base_vector from which
1590 * it is computed.
1591 * Once GOS 6 and GVM 11 are retired, all set_tag commands
1592 * in the NASL scripts can be removed that set "cvss_base".
1593 * Once this happened this exception can be removed from the code.
1594 */
1595 return 0;
1596 }
1597
1598 if (n->tag)
1599 {
1600 gchar *newtag;
1601
1602 newtag =
1603 g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1604 g_free (n->tag);
1605 n->tag = newtag;
1606 }
1607 else
1608 n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1609
1610 g_free (newvalue);
1611
1612 return 0;
1613}
1614
1624int
1625nvti_set_tag (nvti_t *n, const gchar *tag)
1626{
1627 if (!n)
1628 return -1;
1629
1630 g_free (n->tag);
1631 if (tag && tag[0])
1632 n->tag = g_strdup (tag);
1633 else
1634 n->tag = NULL;
1635 return 0;
1636}
1637
1647int
1648nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1649{
1650 if (!n)
1651 return -1;
1652
1653 g_free (n->cvss_base);
1654 if (cvss_base && cvss_base[0])
1655 n->cvss_base = g_strdup (cvss_base);
1656 else
1657 n->cvss_base = NULL;
1658 return 0;
1659}
1660
1671int
1672nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1673{
1674 if (!n)
1675 return -1;
1676
1677 g_free (n->dependencies);
1678 if (dependencies && dependencies[0])
1679 n->dependencies = g_strdup (dependencies);
1680 else
1681 n->dependencies = NULL;
1682 return 0;
1683}
1684
1695int
1696nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1697{
1698 if (!n)
1699 return -1;
1700
1701 g_free (n->required_keys);
1702 if (required_keys && required_keys[0])
1703 n->required_keys = g_strdup (required_keys);
1704 else
1705 n->required_keys = NULL;
1706 return 0;
1707}
1708
1719int
1720nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1721{
1722 if (!n)
1723 return -1;
1724
1725 g_free (n->mandatory_keys);
1726 if (mandatory_keys && mandatory_keys[0])
1727 n->mandatory_keys = g_strdup (mandatory_keys);
1728 else
1729 n->mandatory_keys = NULL;
1730 return 0;
1731}
1732
1743int
1744nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1745{
1746 if (!n)
1747 return -1;
1748
1749 g_free (n->excluded_keys);
1750 if (excluded_keys && excluded_keys[0])
1751 n->excluded_keys = g_strdup (excluded_keys);
1752 else
1753 n->excluded_keys = NULL;
1754 return 0;
1755}
1756
1767int
1768nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1769{
1770 if (!n)
1771 return -1;
1772
1773 g_free (n->required_ports);
1774 if (required_ports && required_ports[0])
1775 n->required_ports = g_strdup (required_ports);
1776 else
1777 n->required_ports = NULL;
1778 return 0;
1779}
1780
1791int
1792nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1793{
1794 if (!n)
1795 return -1;
1796
1797 g_free (n->required_udp_ports);
1798 if (required_udp_ports && required_udp_ports[0])
1799 n->required_udp_ports = g_strdup (required_udp_ports);
1800 else
1801 n->required_udp_ports = NULL;
1802 return 0;
1803}
1804
1814int
1815nvti_set_detection (nvti_t *n, const gchar *detection)
1816{
1817 if (!n)
1818 return -1;
1819
1820 g_free (n->detection);
1821 n->detection = g_strdup (detection);
1822 return 0;
1823}
1824
1834int
1835nvti_put_detection (nvti_t *n, gchar *detection)
1836{
1837 if (!n)
1838 return -1;
1839
1840 g_free (n->detection);
1841 n->detection = detection;
1842 return 0;
1843}
1844
1855int
1856nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1857{
1858 if (!n)
1859 return -1;
1860
1861 g_free (n->qod_type);
1862 if (qod_type && qod_type[0])
1863 n->qod_type = g_strdup (qod_type);
1864 else
1865 n->qod_type = NULL;
1866 return 0;
1867}
1868
1879int
1880nvti_set_qod (nvti_t *n, const gchar *qod)
1881{
1882 if (!n)
1883 return -1;
1884
1885 g_free (n->qod);
1886 if (qod && qod[0])
1887 n->qod = g_strdup (qod);
1888 else
1889 n->qod = NULL;
1890 return 0;
1891}
1892
1902int
1903nvti_set_family (nvti_t *n, const gchar *family)
1904{
1905 if (!n)
1906 return -1;
1907
1908 g_free (n->family);
1909 n->family = g_strdup (family);
1910 return 0;
1911}
1912
1922int
1923nvti_put_family (nvti_t *n, gchar *family)
1924{
1925 if (!n)
1926 return -1;
1927
1928 g_free (n->family);
1929 n->family = family;
1930 return 0;
1931}
1932
1942int
1943nvti_set_category (nvti_t *n, const gint category)
1944{
1945 if (!n)
1946 return -1;
1947
1948 n->category = category;
1949 return 0;
1950}
1951
1967int
1968nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
1969 const gchar *ref_text)
1970{
1971 gchar **split, **item;
1972
1973 if (!n)
1974 return 1;
1975
1976 if (!ref_ids)
1977 return 2;
1978
1979 split = g_strsplit (ref_ids, ",", 0);
1980
1981 for (item = split; *item; item++)
1982 {
1983 gchar *id;
1984
1985 id = *item;
1986 g_strstrip (id);
1987
1988 if (strcmp (id, "") == 0)
1989 continue;
1990
1991 if (type)
1992 {
1993 nvti_add_vtref (n, vtref_new (type, id, ref_text));
1994 }
1995 else
1996 {
1997 gchar **split2;
1998
1999 split2 = g_strsplit (id, ":", 2);
2000 if (split2[0] && split2[1])
2001 nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
2002 g_strfreev (split2);
2003 }
2004 }
2005 g_strfreev (split);
2006
2007 return 0;
2008}
2009
2019int
2020nvti_add_required_keys (nvti_t *n, const gchar *key)
2021{
2022 gchar *old;
2023
2024 if (!n)
2025 return 1;
2026 if (!key)
2027 return 2;
2028
2029 old = n->required_keys;
2030
2031 if (old)
2032 {
2033 n->required_keys = g_strdup_printf ("%s, %s", old, key);
2034 g_free (old);
2035 }
2036 else
2037 n->required_keys = g_strdup (key);
2038
2039 return 0;
2040}
2041
2051int
2052nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
2053{
2054 gchar *old;
2055
2056 if (!n)
2057 return 1;
2058 if (!key)
2059 return 2;
2060
2061 old = n->mandatory_keys;
2062
2063 if (old)
2064 {
2065 n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
2066 g_free (old);
2067 }
2068 else
2069 n->mandatory_keys = g_strdup (key);
2070
2071 return 0;
2072}
2073
2083int
2084nvti_add_excluded_keys (nvti_t *n, const gchar *key)
2085{
2086 gchar *old;
2087
2088 if (!n)
2089 return 1;
2090 if (!key)
2091 return 2;
2092
2093 old = n->excluded_keys;
2094
2095 if (old)
2096 {
2097 n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
2098 g_free (old);
2099 }
2100 else
2101 n->excluded_keys = g_strdup (key);
2102
2103 return 0;
2104}
2105
2115int
2116nvti_add_required_ports (nvti_t *n, const gchar *port)
2117{
2118 gchar *old;
2119
2120 if (!n)
2121 return 1;
2122 if (!port)
2123 return 2;
2124
2125 old = n->required_ports;
2126
2127 if (old)
2128 {
2129 n->required_ports = g_strdup_printf ("%s, %s", old, port);
2130 g_free (old);
2131 }
2132 else
2133 n->required_ports = g_strdup (port);
2134
2135 return 0;
2136}
2137
2147int
2149{
2150 gchar *old;
2151
2152 if (!n)
2153 return 1;
2154 if (!port)
2155 return 2;
2156
2157 old = n->required_udp_ports;
2158
2159 if (old)
2160 {
2161 n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
2162 g_free (old);
2163 }
2164 else
2165 n->required_udp_ports = g_strdup (port);
2166
2167 return 0;
2168}
2169
2179int
2181{
2182 if (!n)
2183 return -1;
2184
2185 n->prefs = g_slist_append (n->prefs, np);
2186 return 0;
2187}
2188
2189/* Collections of nvtis. */
2190
2196static void
2198{
2199 nvti_free ((nvti_t *) nvti);
2200}
2201
2207nvtis_t *
2209{
2210 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2212}
2213
2219void
2221{
2222 if (nvtis)
2223 g_hash_table_destroy (nvtis);
2224}
2225
2232void
2234{
2235 if (nvti)
2236 g_hash_table_insert (
2237 nvtis, (gpointer) (nvti_oid (nvti) ? g_strdup (nvti_oid (nvti)) : NULL),
2238 (gpointer) nvti);
2239}
2240
2249nvti_t *
2250nvtis_lookup (nvtis_t *nvtis, const char *oid)
2251{
2252 return g_hash_table_lookup (nvtis, oid);
2253}
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:737
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:559
int nvti_put_impact(nvti_t *n, gchar *impact)
Set the impact text of a NVT, using the given memory.
Definition nvti.c:1414
void vtseverity_free(vtseverity_t *s)
Free memory of a vtseverity structure.
Definition nvti.c:203
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition nvti.c:1032
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition nvti.c:2020
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition nvti.c:1171
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1880
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition nvti.c:1116
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition nvti.c:1102
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1472
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:982
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition nvti.c:639
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1513
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition nvti.c:1088
int nvti_put_insight(nvti_t *n, gchar *insight)
Set the insight text of a NVT, using the given memory.
Definition nvti.c:1334
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:766
double nvti_severity_score(const nvti_t *n)
Get the maximum severity score.
Definition nvti.c:868
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition nvti.c:532
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition nvti.c:1744
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:1856
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition nvti.c:2084
int nvti_put_detection(nvti_t *n, gchar *detection)
Set the detection text of a NVT, using the given memory.
Definition nvti.c:1835
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2208
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition nvti.c:1672
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition nvti.c:1768
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1214
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition nvti.c:2148
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1625
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1648
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition nvti.c:1720
int nvti_put_name(nvti_t *n, gchar *name)
Set the name of a NVT, using the given memory.
Definition nvti.c:1254
int nvti_put_family(nvti_t *n, gchar *family)
Set the family of a NVT, using the given memory.
Definition nvti.c:1923
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition nvti.c:1199
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1394
const gchar * vtseverity_type(const vtseverity_t *s)
Get the type of a severity.
Definition nvti.c:223
int nvti_add_required_ports(nvti_t *n, const gchar *port)
Add a required port of a NVT.
Definition nvti.c:2116
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:1158
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:900
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:2197
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:952
guint nvti_vtseverities_len(const nvti_t *n)
Get the number of severities of the NVT.
Definition nvti.c:840
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition nvti.c:1186
gchar * nvti_qod(const nvti_t *n)
Get the QoD.
Definition nvti.c:1144
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition nvti.c:1074
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1274
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:625
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:611
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:463
int nvti_put_affected(nvti_t *n, gchar *affected)
Set the affected text of a NVT, using the given memory.
Definition nvti.c:1374
int nvti_put_solution(nvti_t *n, gchar *solution)
Set the solution of a NVT, using the given memory.
Definition nvti.c:1492
double vtseverity_score(const vtseverity_t *s)
Get the score of a severity.
Definition nvti.c:279
const gchar * vtref_type(const vtref_t *r)
Get the type of a reference.
Definition nvti.c:117
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition nvti.c:2180
int nvti_put_summary(nvti_t *n, gchar *summary)
Set the summary of a NVT, using the given memory.
Definition nvti.c:1294
const gchar * vtseverity_value(const vtseverity_t *s)
Get the value of a severity.
Definition nvti.c:251
int nvti_set_insight(nvti_t *n, const gchar *insight)
Set the insight text of a NVT.
Definition nvti.c:1314
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:77
gchar * nvti_cvss_base(const nvti_t *n)
Get the CVSS base.
Definition nvti.c:1018
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition nvti.c:1696
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:426
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition nvti.c:484
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1534
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition nvti.c:504
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1234
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2250
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition nvti.c:1130
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition nvti.c:2052
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition nvti.c:695
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition nvti.c:667
vtseverity_t * nvti_vtseverity(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:855
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:180
gchar * nvti_solution(const nvti_t *n)
Get the solution.
Definition nvti.c:924
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1453
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1434
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition nvti.c:1046
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition nvti.c:653
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:1968
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition nvti.c:709
void vtref_free(vtref_t *ref)
Free memory of a vtref structure.
Definition nvti.c:97
const gchar * vtseverity_origin(const vtseverity_t *s)
Get the origin of a severity.
Definition nvti.c:237
guint nvti_vtref_len(const nvti_t *n)
Get the number of references of the NVT.
Definition nvti.c:722
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:1792
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition nvti.c:1060
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition nvti.c:546
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1903
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition nvti.c:518
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:295
int nvti_set_affected(nvti_t *n, const gchar *affected)
Set the affected text of a NVT.
Definition nvti.c:1354
int vtseverity_date(const vtseverity_t *s)
Get the date of a severity.
Definition nvti.c:265
gchar * nvti_solution_type(const nvti_t *n)
Get the solution type.
Definition nvti.c:938
const gchar * vtref_id(const vtref_t *r)
Get the id of a reference.
Definition nvti.c:131
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:1561
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2233
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition nvti.c:681
const gchar * vtref_text(const vtref_t *r)
Get the text of a reference.
Definition nvti.c:145
void nvtis_free(nvtis_t *nvtis)
Free a collection of NVT Infos.
Definition nvti.c:2220
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1815
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition nvti.c:966
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1943
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:570
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition nvti.c:408
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:358
gchar * solution_method
The solution method.
Definition nvti.c:372
gchar * insight
The insight.
Definition nvti.c:363
gchar * affected
Affected systems.
Definition nvti.c:364
gchar * cvss_base
CVSS base score for this NVT.
Definition nvti.c:375
gchar * required_keys
List of required KB keys of this NVT.
Definition nvti.c:378
time_t creation_time
Time of creation, seconds since epoch.
Definition nvti.c:367
gchar * solution
The solution.
Definition nvti.c:370
gchar * impact
Impact of vulnerability.
Definition nvti.c:365
gchar * family
Family the NVT belongs to.
Definition nvti.c:395
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition nvti.c:380
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition nvti.c:383
GSList * refs
Collection of VT references.
Definition nvti.c:389
gchar * tag
List of tags attached to this NVT.
Definition nvti.c:374
gchar * oid
Object ID.
Definition nvti.c:359
gchar * name
The name.
Definition nvti.c:360
gint category
The category, this NVT belongs to.
Definition nvti.c:394
gchar * qod_type
Quality of detection type.
Definition nvti.c:386
GSList * severities
Collection of VT severities.
Definition nvti.c:390
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition nvti.c:379
gchar * detection
Detection description.
Definition nvti.c:385
gchar * solution_type
The solution type.
Definition nvti.c:371
gchar * dependencies
List of dependencies of this NVT.
Definition nvti.c:377
gchar * qod
Quality of detection.
Definition nvti.c:387
time_t modification_time
Time of last change, sec. since epoch.
Definition nvti.c:368
gchar * required_ports
List of required ports of this NVT.
Definition nvti.c:381
gchar * summary
The summary.
Definition nvti.c:362
GSList * prefs
Collection of NVT preferences.
Definition nvti.c:391
The structure for a preference of a NVT.
Definition nvti.c:441
gchar * type
Preference type.
Definition nvti.c:443
gchar * dflt
Default value of the preference.
Definition nvti.c:445
int id
Preference ID.
Definition nvti.c:442
gchar * name
Name of the preference.
Definition nvti.c:444
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:158
gchar * origin
Definition nvti.c:160
int date
Timestamp in seconds since epoch, defaults to VT creation date.
Definition nvti.c:162
double score
The score derived from the value in range [0.0-10.0].
Definition nvti.c:163
gchar * value
The value which corresponds to the type.
Definition nvti.c:164
gchar * type
Severity type ("cvss_base_v2", ...)
Definition nvti.c:159