Greenbone Vulnerability Management Libraries 22.10.0
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 return 0;
305
306 /* Parse the time. */
307
308 /* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
309 memset (&tm, 0, sizeof (struct tm));
310 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)
311 {
312 memset (&tm, 0, sizeof (struct tm));
313 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)
314 {
315 g_warning ("%s: Failed to parse time: %s", __func__, str_time);
316 return 0;
317 }
318 }
319 epoch_time = mktime (&tm);
320 if (epoch_time == -1)
321 {
322 g_warning ("%s: Failed to make time: %s", __func__, str_time);
323 return 0;
324 }
325
326 /* Get the timezone offset from the str_time. */
327
328 if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
329 != 1)
330 && (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
331 &offset)
332 != 1))
333 {
334 g_warning ("%s: Failed to parse timezone offset: %s", __func__, str_time);
335 return 0;
336 }
337
338 /* Use the offset to convert to UTC. */
339
340 if (offset < 0)
341 {
342 epoch_time += ((-offset) / 100) * 60 * 60;
343 epoch_time += ((-offset) % 100) * 60;
344 }
345 else if (offset > 0)
346 {
347 epoch_time -= (offset / 100) * 60 * 60;
348 epoch_time -= (offset % 100) * 60;
349 }
350
351 return epoch_time;
352}
353
354/* VT Information */
355
359typedef struct nvti
360{
361 gchar *oid;
362 gchar *name;
364 gchar *summary;
365 gchar *insight;
366 gchar *affected;
367 gchar *impact;
372 gchar *solution;
376 gchar *tag;
377 gchar *cvss_base;
384 gchar
387 gchar *detection;
388 gchar *qod_type;
389 gchar *qod;
391 GSList *refs;
392 GSList *severities;
393 GSList *prefs;
395 // The following are not settled yet.
396 gint category;
397 gchar *family;
399
409int
411{
412 if (!vt)
413 return -1;
414
415 vt->refs = g_slist_append (vt->refs, ref);
416 return 0;
417}
418
427int
429{
430 if (!vt)
431 return -1;
432
433 vt->severities = g_slist_append (vt->severities, s);
434 return 0;
435}
436
437/* VT preferences */
438
442typedef struct nvtpref
443{
444 int id;
445 gchar *type;
446 gchar *name;
447 gchar *dflt;
449
465nvtpref_t *
466nvtpref_new (int id, const gchar *name, const gchar *type, const gchar *dflt)
467{
468 nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
469
470 np->id = id;
471 if (name)
472 np->name = g_strdup (name);
473 if (type)
474 np->type = g_strdup (type);
475 if (dflt)
476 np->dflt = g_strdup (dflt);
477
478 return np;
479}
480
486void
488{
489 if (!np)
490 return;
491
492 g_free (np->name);
493 g_free (np->type);
494 g_free (np->dflt);
495 g_free (np);
496}
497
506int
508{
509 return np ? np->id : -1;
510}
511
520gchar *
522{
523 return np ? np->name : NULL;
524}
525
534gchar *
536{
537 return np ? np->type : NULL;
538}
539
548gchar *
550{
551 return np ? np->dflt : NULL;
552}
553
562nvti_t *
564{
565 return (nvti_t *) g_malloc0 (sizeof (nvti_t));
566}
567
573void
575{
576 if (!n)
577 return;
578
579 g_free (n->oid);
580 g_free (n->name);
581 g_free (n->summary);
582 g_free (n->insight);
583 g_free (n->affected);
584 g_free (n->impact);
585 g_free (n->solution);
586 g_free (n->solution_type);
587 g_free (n->solution_method);
588 g_free (n->tag);
589 g_free (n->cvss_base);
590 g_free (n->dependencies);
591 g_free (n->required_keys);
592 g_free (n->mandatory_keys);
593 g_free (n->excluded_keys);
594 g_free (n->required_ports);
595 g_free (n->required_udp_ports);
596 g_free (n->detection);
597 g_free (n->qod_type);
598 g_free (n->qod);
599 g_free (n->family);
600 g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
601 g_slist_free_full (n->severities, (void (*) (void *)) vtseverity_free);
602 g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
603 g_free (n);
604}
605
614gchar *
615nvti_oid (const nvti_t *n)
616{
617 return n ? n->oid : NULL;
618}
619
628gchar *
630{
631 return n ? n->name : NULL;
632}
633
642gchar *
644{
645 return n ? n->summary : NULL;
646}
647
656gchar *
658{
659 return n ? n->insight : NULL;
660}
661
670gchar *
672{
673 return n ? n->affected : NULL;
674}
675
684gchar *
686{
687 return n ? n->impact : NULL;
688}
689
698time_t
700{
701 return n ? n->creation_time : 0;
702}
703
712time_t
714{
715 return n ? n->modification_time : 0;
716}
717
725guint
727{
728 return n ? g_slist_length (n->refs) : 0;
729}
730
740vtref_t *
741nvti_vtref (const nvti_t *n, guint p)
742{
743 return n ? g_slist_nth_data (n->refs, p) : NULL;
744}
745
769gchar *
770nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
771 guint use_types)
772{
773 gchar *refs, *refs2, **exclude_item;
774 vtref_t *ref;
775 guint i, exclude;
776 gchar **exclude_split;
777
778 if (!n)
779 return NULL;
780
781 refs = NULL;
782 refs2 = NULL;
783 exclude = 0;
784
785 if (exclude_types && exclude_types[0])
786 exclude_split = g_strsplit (exclude_types, ",", 0);
787 else
788 exclude_split = NULL;
789
790 for (i = 0; i < g_slist_length (n->refs); i++)
791 {
792 ref = g_slist_nth_data (n->refs, i);
793 if (type && strcasecmp (ref->type, type) != 0)
794 continue;
795
796 if (exclude_split)
797 {
798 exclude = 0;
799 for (exclude_item = exclude_split; *exclude_item; exclude_item++)
800 {
801 if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
802 {
803 exclude = 1;
804 break;
805 }
806 }
807 }
808
809 if (!exclude)
810 {
811 if (use_types)
812 {
813 if (refs)
814 refs2 =
815 g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
816 else
817 refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
818 }
819 else
820 {
821 if (refs)
822 refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
823 else
824 refs2 = g_strdup_printf ("%s", ref->ref_id);
825 }
826 g_free (refs);
827 refs = refs2;
828 }
829 }
830
831 g_strfreev (exclude_split);
832
833 return refs;
834}
835
843guint
845{
846 return n ? g_slist_length (n->severities) : 0;
847}
848
859nvti_vtseverity (const nvti_t *n, guint p)
860{
861 return n ? g_slist_nth_data (n->severities, p) : NULL;
862}
863
871double
873{
874 unsigned int i;
875 double score = -1.0;
876
877 for (i = 0; i < nvti_vtseverities_len (n); i++)
878 {
879 vtseverity_t *severity;
880
881 severity = nvti_vtseverity (n, i);
882 if (vtseverity_score (severity) > score)
883 score = vtseverity_score (severity);
884 }
885
886 return score;
887}
888
903gchar *
905{
906 gchar *vector;
907
908 /* Currently, only one severity_vector can be stored as tag.
909 * Therfore we just check this one. */
910 vector = nvti_get_tag (n, "severity_vector");
911 if (vector)
912 return vector;
913
914 vector = nvti_get_tag (n, "cvss_base_vector");
915
916 return vector;
917}
918
927gchar *
929{
930 return n ? n->solution : NULL;
931}
932
941gchar *
943{
944 return n ? n->solution_type : NULL;
945}
946
955gchar *
957{
958 return n ? n->solution_method : NULL;
959}
960
969gchar *
970nvti_tag (const nvti_t *n)
971{
972 return n ? n->tag : NULL;
973}
974
985gchar *
986nvti_get_tag (const nvti_t *n, const gchar *name)
987{
988 gchar **split, **point;
989
990 if (!n || n->tag == NULL || !name)
991 return NULL;
992
993 split = g_strsplit (n->tag, "|", 0);
994 point = split;
995
996 while (*point)
997 {
998 if ((strlen (*point) > strlen (name))
999 && (strncmp (*point, name, strlen (name)) == 0)
1000 && ((*point)[strlen (name)] == '='))
1001 {
1002 gchar *ret;
1003 ret = g_strdup (*point + strlen (name) + 1);
1004 g_strfreev (split);
1005 return ret;
1006 }
1007 point++;
1008 }
1009 g_strfreev (split);
1010 return NULL;
1011}
1012
1021gchar *
1023{
1024 return n ? n->cvss_base : NULL;
1025}
1026
1035gchar *
1037{
1038 return n ? n->dependencies : NULL;
1039}
1040
1049gchar *
1051{
1052 return n ? n->required_keys : NULL;
1053}
1054
1063gchar *
1065{
1066 return n ? n->mandatory_keys : NULL;
1067}
1068
1077gchar *
1079{
1080 return n ? n->excluded_keys : NULL;
1081}
1082
1091gchar *
1093{
1094 return n ? n->required_ports : NULL;
1095}
1096
1105gchar *
1107{
1108 return n ? n->required_udp_ports : NULL;
1109}
1110
1119gchar *
1121{
1122 return n ? n->detection : NULL;
1123}
1124
1133gchar *
1135{
1136 return n ? n->qod_type : NULL;
1137}
1138
1147gchar *
1149{
1150 return n ? n->qod : NULL;
1151}
1152
1161gchar *
1163{
1164 return n ? n->family : NULL;
1165}
1166
1174guint
1176{
1177 return n ? g_slist_length (n->prefs) : 0;
1178}
1179
1189const nvtpref_t *
1190nvti_pref (const nvti_t *n, guint p)
1191{
1192 return n ? g_slist_nth_data (n->prefs, p) : NULL;
1193}
1194
1202gint
1204{
1205 return n ? n->category : -1;
1206}
1207
1217int
1218nvti_set_oid (nvti_t *n, const gchar *oid)
1219{
1220 if (!n)
1221 return -1;
1222
1223 g_free (n->oid);
1224 n->oid = g_strdup (oid);
1225 return 0;
1226}
1227
1237int
1238nvti_set_name (nvti_t *n, const gchar *name)
1239{
1240 if (!n)
1241 return -1;
1242
1243 g_free (n->name);
1244 n->name = g_strdup (name);
1245 return 0;
1246}
1247
1257int
1258nvti_put_name (nvti_t *n, gchar *name)
1259{
1260 if (!n)
1261 return -1;
1262
1263 g_free (n->name);
1264 n->name = name;
1265 return 0;
1266}
1267
1277int
1278nvti_set_summary (nvti_t *n, const gchar *summary)
1279{
1280 if (!n)
1281 return -1;
1282
1283 g_free (n->summary);
1284 n->summary = g_strdup (summary);
1285 return 0;
1286}
1287
1297int
1298nvti_put_summary (nvti_t *n, gchar *summary)
1299{
1300 if (!n)
1301 return -1;
1302
1303 g_free (n->summary);
1304 n->summary = summary;
1305 return 0;
1306}
1307
1317int
1318nvti_set_insight (nvti_t *n, const gchar *insight)
1319{
1320 if (!n)
1321 return -1;
1322
1323 g_free (n->insight);
1324 n->insight = g_strdup (insight);
1325 return 0;
1326}
1327
1337int
1338nvti_put_insight (nvti_t *n, gchar *insight)
1339{
1340 if (!n)
1341 return -1;
1342
1343 g_free (n->insight);
1344 n->insight = insight;
1345 return 0;
1346}
1347
1357int
1358nvti_set_affected (nvti_t *n, const gchar *affected)
1359{
1360 if (!n)
1361 return -1;
1362
1363 g_free (n->affected);
1364 n->affected = g_strdup (affected);
1365 return 0;
1366}
1367
1377int
1378nvti_put_affected (nvti_t *n, gchar *affected)
1379{
1380 if (!n)
1381 return -1;
1382
1383 g_free (n->affected);
1384 n->affected = affected;
1385 return 0;
1386}
1387
1397int
1398nvti_set_impact (nvti_t *n, const gchar *impact)
1399{
1400 if (!n)
1401 return -1;
1402
1403 g_free (n->impact);
1404 n->impact = g_strdup (impact);
1405 return 0;
1406}
1407
1417int
1418nvti_put_impact (nvti_t *n, gchar *impact)
1419{
1420 if (!n)
1421 return -1;
1422
1423 g_free (n->impact);
1424 n->impact = impact;
1425 return 0;
1426}
1427
1437int
1438nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1439{
1440 if (!n)
1441 return -1;
1442
1443 n->creation_time = creation_time;
1444 return 0;
1445}
1446
1456int
1457nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1458{
1459 if (!n)
1460 return -1;
1461
1462 n->modification_time = modification_time;
1463 return 0;
1464}
1465
1475int
1476nvti_set_solution (nvti_t *n, const gchar *solution)
1477{
1478 if (!n)
1479 return -1;
1480
1481 g_free (n->solution);
1482 n->solution = g_strdup (solution);
1483 return 0;
1484}
1485
1495int
1496nvti_put_solution (nvti_t *n, gchar *solution)
1497{
1498 if (!n)
1499 return -1;
1500
1501 g_free (n->solution);
1502 n->solution = solution;
1503 return 0;
1504}
1505
1516int
1517nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1518{
1519 if (!n)
1520 return -1;
1521
1522 g_free (n->solution_type);
1523 n->solution_type = g_strdup (solution_type);
1524 return 0;
1525}
1526
1537int
1538nvti_set_solution_method (nvti_t *n, const gchar *solution_method)
1539{
1540 if (!n)
1541 return -1;
1542
1543 g_free (n->solution_method);
1544 n->solution_method = g_strdup (solution_method);
1545 return 0;
1546}
1547
1564int
1565nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1566{
1567 gchar *newvalue = NULL;
1568
1569 if (!n)
1570 return -1;
1571
1572 if (!name || !name[0])
1573 return -2;
1574
1575 if (!value || !value[0])
1576 return -3;
1577
1578 if (!strcmp (name, "last_modification"))
1579 {
1581 newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1582 }
1583 else if (!strcmp (name, "creation_date"))
1584 {
1586 newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1587 }
1588 else if (!strcmp (name, "severity_date"))
1589 newvalue = g_strdup_printf ("%i", (int) parse_nvt_timestamp (value));
1590 else if (!strcmp (name, "cvss_base"))
1591 {
1592 /* Ignore this tag because it is not being used.
1593 * It is redundant with the tag cvss_base_vector from which
1594 * it is computed.
1595 * Once GOS 6 and GVM 11 are retired, all set_tag commands
1596 * in the NASL scripts can be removed that set "cvss_base".
1597 * Once this happened this exception can be removed from the code.
1598 */
1599 return 0;
1600 }
1601
1602 if (n->tag)
1603 {
1604 gchar *newtag;
1605
1606 newtag =
1607 g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1608 g_free (n->tag);
1609 n->tag = newtag;
1610 }
1611 else
1612 n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1613
1614 g_free (newvalue);
1615
1616 return 0;
1617}
1618
1628int
1629nvti_set_tag (nvti_t *n, const gchar *tag)
1630{
1631 if (!n)
1632 return -1;
1633
1634 g_free (n->tag);
1635 if (tag && tag[0])
1636 n->tag = g_strdup (tag);
1637 else
1638 n->tag = NULL;
1639 return 0;
1640}
1641
1651int
1652nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1653{
1654 if (!n)
1655 return -1;
1656
1657 g_free (n->cvss_base);
1658 if (cvss_base && cvss_base[0])
1659 n->cvss_base = g_strdup (cvss_base);
1660 else
1661 n->cvss_base = NULL;
1662 return 0;
1663}
1664
1675int
1676nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1677{
1678 if (!n)
1679 return -1;
1680
1681 g_free (n->dependencies);
1682 if (dependencies && dependencies[0])
1683 n->dependencies = g_strdup (dependencies);
1684 else
1685 n->dependencies = NULL;
1686 return 0;
1687}
1688
1699int
1700nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1701{
1702 if (!n)
1703 return -1;
1704
1705 g_free (n->required_keys);
1706 if (required_keys && required_keys[0])
1707 n->required_keys = g_strdup (required_keys);
1708 else
1709 n->required_keys = NULL;
1710 return 0;
1711}
1712
1723int
1724nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1725{
1726 if (!n)
1727 return -1;
1728
1729 g_free (n->mandatory_keys);
1730 if (mandatory_keys && mandatory_keys[0])
1731 n->mandatory_keys = g_strdup (mandatory_keys);
1732 else
1733 n->mandatory_keys = NULL;
1734 return 0;
1735}
1736
1747int
1748nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1749{
1750 if (!n)
1751 return -1;
1752
1753 g_free (n->excluded_keys);
1754 if (excluded_keys && excluded_keys[0])
1755 n->excluded_keys = g_strdup (excluded_keys);
1756 else
1757 n->excluded_keys = NULL;
1758 return 0;
1759}
1760
1771int
1772nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1773{
1774 if (!n)
1775 return -1;
1776
1777 g_free (n->required_ports);
1778 if (required_ports && required_ports[0])
1779 n->required_ports = g_strdup (required_ports);
1780 else
1781 n->required_ports = NULL;
1782 return 0;
1783}
1784
1795int
1796nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1797{
1798 if (!n)
1799 return -1;
1800
1801 g_free (n->required_udp_ports);
1802 if (required_udp_ports && required_udp_ports[0])
1803 n->required_udp_ports = g_strdup (required_udp_ports);
1804 else
1805 n->required_udp_ports = NULL;
1806 return 0;
1807}
1808
1818int
1819nvti_set_detection (nvti_t *n, const gchar *detection)
1820{
1821 if (!n)
1822 return -1;
1823
1824 g_free (n->detection);
1825 n->detection = g_strdup (detection);
1826 return 0;
1827}
1828
1838int
1839nvti_put_detection (nvti_t *n, gchar *detection)
1840{
1841 if (!n)
1842 return -1;
1843
1844 g_free (n->detection);
1845 n->detection = detection;
1846 return 0;
1847}
1848
1859int
1860nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1861{
1862 if (!n)
1863 return -1;
1864
1865 g_free (n->qod_type);
1866 if (qod_type && qod_type[0])
1867 n->qod_type = g_strdup (qod_type);
1868 else
1869 n->qod_type = NULL;
1870 return 0;
1871}
1872
1883int
1884nvti_set_qod (nvti_t *n, const gchar *qod)
1885{
1886 if (!n)
1887 return -1;
1888
1889 g_free (n->qod);
1890 if (qod && qod[0])
1891 n->qod = g_strdup (qod);
1892 else
1893 n->qod = NULL;
1894 return 0;
1895}
1896
1906int
1907nvti_set_family (nvti_t *n, const gchar *family)
1908{
1909 if (!n)
1910 return -1;
1911
1912 g_free (n->family);
1913 n->family = g_strdup (family);
1914 return 0;
1915}
1916
1926int
1927nvti_put_family (nvti_t *n, gchar *family)
1928{
1929 if (!n)
1930 return -1;
1931
1932 g_free (n->family);
1933 n->family = family;
1934 return 0;
1935}
1936
1946int
1947nvti_set_category (nvti_t *n, const gint category)
1948{
1949 if (!n)
1950 return -1;
1951
1952 n->category = category;
1953 return 0;
1954}
1955
1971int
1972nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
1973 const gchar *ref_text)
1974{
1975 gchar **split, **item;
1976
1977 if (!n)
1978 return 1;
1979
1980 if (!ref_ids)
1981 return 2;
1982
1983 split = g_strsplit (ref_ids, ",", 0);
1984
1985 for (item = split; *item; item++)
1986 {
1987 gchar *id;
1988
1989 id = *item;
1990 g_strstrip (id);
1991
1992 if (strcmp (id, "") == 0)
1993 continue;
1994
1995 if (type)
1996 {
1997 nvti_add_vtref (n, vtref_new (type, id, ref_text));
1998 }
1999 else
2000 {
2001 gchar **split2;
2002
2003 split2 = g_strsplit (id, ":", 2);
2004 if (split2[0] && split2[1])
2005 nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
2006 g_strfreev (split2);
2007 }
2008 }
2009 g_strfreev (split);
2010
2011 return 0;
2012}
2013
2023int
2024nvti_add_required_keys (nvti_t *n, const gchar *key)
2025{
2026 gchar *old;
2027
2028 if (!n)
2029 return 1;
2030 if (!key)
2031 return 2;
2032
2033 old = n->required_keys;
2034
2035 if (old)
2036 {
2037 n->required_keys = g_strdup_printf ("%s, %s", old, key);
2038 g_free (old);
2039 }
2040 else
2041 n->required_keys = g_strdup (key);
2042
2043 return 0;
2044}
2045
2055int
2056nvti_add_mandatory_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->mandatory_keys;
2066
2067 if (old)
2068 {
2069 n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
2070 g_free (old);
2071 }
2072 else
2073 n->mandatory_keys = g_strdup (key);
2074
2075 return 0;
2076}
2077
2087int
2088nvti_add_excluded_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->excluded_keys;
2098
2099 if (old)
2100 {
2101 n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
2102 g_free (old);
2103 }
2104 else
2105 n->excluded_keys = g_strdup (key);
2106
2107 return 0;
2108}
2109
2119int
2120nvti_add_required_ports (nvti_t *n, const gchar *port)
2121{
2122 gchar *old;
2123
2124 if (!n)
2125 return 1;
2126 if (!port)
2127 return 2;
2128
2129 old = n->required_ports;
2130
2131 if (old)
2132 {
2133 n->required_ports = g_strdup_printf ("%s, %s", old, port);
2134 g_free (old);
2135 }
2136 else
2137 n->required_ports = g_strdup (port);
2138
2139 return 0;
2140}
2141
2151int
2153{
2154 gchar *old;
2155
2156 if (!n)
2157 return 1;
2158 if (!port)
2159 return 2;
2160
2161 old = n->required_udp_ports;
2162
2163 if (old)
2164 {
2165 n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
2166 g_free (old);
2167 }
2168 else
2169 n->required_udp_ports = g_strdup (port);
2170
2171 return 0;
2172}
2173
2183int
2185{
2186 if (!n)
2187 return -1;
2188
2189 n->prefs = g_slist_append (n->prefs, np);
2190 return 0;
2191}
2192
2193/* Collections of nvtis. */
2194
2200static void
2202{
2203 nvti_free ((nvti_t *) nvti);
2204}
2205
2211nvtis_t *
2213{
2214 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2216}
2217
2223void
2225{
2226 if (nvtis)
2227 g_hash_table_destroy (nvtis);
2228}
2229
2236void
2238{
2239 if (nvti)
2240 g_hash_table_insert (
2241 nvtis, (gpointer) (nvti_oid (nvti) ? g_strdup (nvti_oid (nvti)) : NULL),
2242 (gpointer) nvti);
2243}
2244
2253nvti_t *
2254nvtis_lookup (nvtis_t *nvtis, const char *oid)
2255{
2256 return g_hash_table_lookup (nvtis, oid);
2257}
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:741
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:563
int nvti_put_impact(nvti_t *n, gchar *impact)
Set the impact text of a NVT, using the given memory.
Definition nvti.c:1418
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:1036
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition nvti.c:2024
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition nvti.c:1175
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1884
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition nvti.c:1120
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition nvti.c:1106
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1476
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:986
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition nvti.c:643
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1517
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition nvti.c:1092
int nvti_put_insight(nvti_t *n, gchar *insight)
Set the insight text of a NVT, using the given memory.
Definition nvti.c:1338
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:770
double nvti_severity_score(const nvti_t *n)
Get the maximum severity score.
Definition nvti.c:872
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition nvti.c:535
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition nvti.c:1748
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:1860
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition nvti.c:2088
int nvti_put_detection(nvti_t *n, gchar *detection)
Set the detection text of a NVT, using the given memory.
Definition nvti.c:1839
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2212
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition nvti.c:1676
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition nvti.c:1772
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1218
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition nvti.c:2152
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1629
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1652
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition nvti.c:1724
int nvti_put_name(nvti_t *n, gchar *name)
Set the name of a NVT, using the given memory.
Definition nvti.c:1258
int nvti_put_family(nvti_t *n, gchar *family)
Set the family of a NVT, using the given memory.
Definition nvti.c:1927
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition nvti.c:1203
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1398
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:2120
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:1162
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:904
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:2201
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:956
guint nvti_vtseverities_len(const nvti_t *n)
Get the number of severities of the NVT.
Definition nvti.c:844
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition nvti.c:1190
gchar * nvti_qod(const nvti_t *n)
Get the QoD.
Definition nvti.c:1148
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition nvti.c:1078
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1278
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:629
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:615
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:466
int nvti_put_affected(nvti_t *n, gchar *affected)
Set the affected text of a NVT, using the given memory.
Definition nvti.c:1378
int nvti_put_solution(nvti_t *n, gchar *solution)
Set the solution of a NVT, using the given memory.
Definition nvti.c:1496
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:2184
int nvti_put_summary(nvti_t *n, gchar *summary)
Set the summary of a NVT, using the given memory.
Definition nvti.c:1298
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:1318
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:1022
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition nvti.c:1700
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:428
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition nvti.c:487
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1538
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition nvti.c:507
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1238
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2254
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition nvti.c:1134
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition nvti.c:2056
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition nvti.c:699
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition nvti.c:671
vtseverity_t * nvti_vtseverity(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:859
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:928
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1457
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1438
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition nvti.c:1050
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition nvti.c:657
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:1972
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition nvti.c:713
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:726
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:1796
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition nvti.c:1064
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition nvti.c:549
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1907
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition nvti.c:521
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:1358
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:942
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:1565
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2237
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition nvti.c:685
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:2224
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1819
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition nvti.c:970
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1947
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:574
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition nvti.c:410
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:360
gchar * solution_method
The solution method.
Definition nvti.c:374
gchar * insight
The insight.
Definition nvti.c:365
gchar * affected
Affected systems.
Definition nvti.c:366
gchar * cvss_base
CVSS base score for this NVT.
Definition nvti.c:377
gchar * required_keys
List of required KB keys of this NVT.
Definition nvti.c:380
time_t creation_time
Time of creation, seconds since epoch.
Definition nvti.c:369
gchar * solution
The solution.
Definition nvti.c:372
gchar * impact
Impact of vulnerability.
Definition nvti.c:367
gchar * family
Family the NVT belongs to.
Definition nvti.c:397
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition nvti.c:382
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition nvti.c:385
GSList * refs
Collection of VT references.
Definition nvti.c:391
gchar * tag
List of tags attached to this NVT.
Definition nvti.c:376
gchar * oid
Object ID.
Definition nvti.c:361
gchar * name
The name.
Definition nvti.c:362
gint category
The category, this NVT belongs to.
Definition nvti.c:396
gchar * qod_type
Quality of detection type.
Definition nvti.c:388
GSList * severities
Collection of VT severities.
Definition nvti.c:392
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition nvti.c:381
gchar * detection
Detection description.
Definition nvti.c:387
gchar * solution_type
The solution type.
Definition nvti.c:373
gchar * dependencies
List of dependencies of this NVT.
Definition nvti.c:379
gchar * qod
Quality of detection.
Definition nvti.c:389
time_t modification_time
Time of last change, sec. since epoch.
Definition nvti.c:370
gchar * required_ports
List of required ports of this NVT.
Definition nvti.c:383
gchar * summary
The summary.
Definition nvti.c:364
GSList * prefs
Collection of NVT preferences.
Definition nvti.c:393
The structure for a preference of a NVT.
Definition nvti.c:443
gchar * type
Preference type.
Definition nvti.c:445
gchar * dflt
Default value of the preference.
Definition nvti.c:447
int id
Preference ID.
Definition nvti.c:444
gchar * name
Name of the preference.
Definition nvti.c:446
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