Greenbone Vulnerability Management Libraries 22.8.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 || (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
458int
460{
461 if (!vt)
462 return -1;
463
464 vt->severities = g_slist_append (vt->severities, s);
465 return 0;
466}
467
468/* VT preferences */
469
473typedef struct nvtpref
474{
475 int id;
476 gchar *type;
477 gchar *name;
478 gchar *dflt;
480
496nvtpref_t *
497nvtpref_new (int id, const gchar *name, const gchar *type, const gchar *dflt)
498{
499 nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
500
501 np->id = id;
502 if (name)
503 np->name = g_strdup (name);
504 if (type)
505 np->type = g_strdup (type);
506 if (dflt)
507 np->dflt = g_strdup (dflt);
508
509 return np;
510}
511
517void
519{
520 if (!np)
521 return;
522
523 g_free (np->name);
524 g_free (np->type);
525 g_free (np->dflt);
526 g_free (np);
527}
528
537int
539{
540 return np ? np->id : -1;
541}
542
551gchar *
553{
554 return np ? np->name : NULL;
555}
556
565gchar *
567{
568 return np ? np->type : NULL;
569}
570
579gchar *
581{
582 return np ? np->dflt : NULL;
583}
584
593nvti_t *
595{
596 return (nvti_t *) g_malloc0 (sizeof (nvti_t));
597}
598
604void
606{
607 if (!n)
608 return;
609
610 g_free (n->oid);
611 g_free (n->name);
612 g_free (n->summary);
613 g_free (n->insight);
614 g_free (n->affected);
615 g_free (n->impact);
616 g_free (n->solution);
617 g_free (n->solution_type);
618 g_free (n->solution_method);
619 g_free (n->tag);
620 g_free (n->cvss_base);
621 g_free (n->dependencies);
622 g_free (n->required_keys);
623 g_free (n->mandatory_keys);
624 g_free (n->excluded_keys);
625 g_free (n->required_ports);
626 g_free (n->required_udp_ports);
627 g_free (n->detection);
628 g_free (n->qod_type);
629 g_free (n->qod);
630 g_free (n->family);
631 g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
632 g_slist_free_full (n->severities, (void (*) (void *)) vtseverity_free);
633 g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
634 g_free (n);
635}
636
645gchar *
646nvti_oid (const nvti_t *n)
647{
648 return n ? n->oid : NULL;
649}
650
659gchar *
661{
662 return n ? n->name : NULL;
663}
664
673gchar *
675{
676 return n ? n->summary : NULL;
677}
678
687gchar *
689{
690 return n ? n->insight : NULL;
691}
692
701gchar *
703{
704 return n ? n->affected : NULL;
705}
706
715gchar *
717{
718 return n ? n->impact : NULL;
719}
720
729time_t
731{
732 return n ? n->creation_time : 0;
733}
734
743time_t
745{
746 return n ? n->modification_time : 0;
747}
748
756guint
758{
759 return n ? g_slist_length (n->refs) : 0;
760}
761
771vtref_t *
772nvti_vtref (const nvti_t *n, guint p)
773{
774 return n ? g_slist_nth_data (n->refs, p) : NULL;
775}
776
800gchar *
801nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
802 guint use_types)
803{
804 gchar *refs, *refs2, **exclude_item;
805 vtref_t *ref;
806 guint i, exclude;
807 gchar **exclude_split;
808
809 if (!n)
810 return NULL;
811
812 refs = NULL;
813 refs2 = NULL;
814 exclude = 0;
815
816 if (exclude_types && exclude_types[0])
817 exclude_split = g_strsplit (exclude_types, ",", 0);
818 else
819 exclude_split = NULL;
820
821 for (i = 0; i < g_slist_length (n->refs); i++)
822 {
823 ref = g_slist_nth_data (n->refs, i);
824 if (type && strcasecmp (ref->type, type) != 0)
825 continue;
826
827 if (exclude_split)
828 {
829 exclude = 0;
830 for (exclude_item = exclude_split; *exclude_item; exclude_item++)
831 {
832 if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
833 {
834 exclude = 1;
835 break;
836 }
837 }
838 }
839
840 if (!exclude)
841 {
842 if (use_types)
843 {
844 if (refs)
845 refs2 =
846 g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
847 else
848 refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
849 }
850 else
851 {
852 if (refs)
853 refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
854 else
855 refs2 = g_strdup_printf ("%s", ref->ref_id);
856 }
857 g_free (refs);
858 refs = refs2;
859 }
860 }
861
862 g_strfreev (exclude_split);
863
864 return refs;
865}
866
874guint
876{
877 return n ? g_slist_length (n->severities) : 0;
878}
879
890nvti_vtseverity (const nvti_t *n, guint p)
891{
892 return n ? g_slist_nth_data (n->severities, p) : NULL;
893}
894
902double
904{
905 unsigned int i;
906 double score = -1.0;
907
908 for (i = 0; i < nvti_vtseverities_len (n); i++)
909 {
910 vtseverity_t *severity;
911
912 severity = nvti_vtseverity (n, i);
913 if (vtseverity_score (severity) > score)
914 score = vtseverity_score (severity);
915 }
916
917 return score;
918}
919
934gchar *
936{
937 gchar *vector;
938
939 /* Currently, only one severity_vector can be stored as tag.
940 * Therfore we just check this one. */
941 vector = nvti_get_tag (n, "severity_vector");
942 if (vector)
943 return vector;
944
945 vector = nvti_get_tag (n, "cvss_base_vector");
946
947 return vector;
948}
949
958gchar *
960{
961 return n ? n->solution : NULL;
962}
963
972gchar *
974{
975 return n ? n->solution_type : NULL;
976}
977
986gchar *
988{
989 return n ? n->solution_method : NULL;
990}
991
1000gchar *
1002{
1003 return n ? n->tag : NULL;
1004}
1005
1016gchar *
1017nvti_get_tag (const nvti_t *n, const gchar *name)
1018{
1019 gchar **split, **point;
1020
1021 if (!n || n->tag == NULL || !name)
1022 return NULL;
1023
1024 split = g_strsplit (n->tag, "|", 0);
1025 point = split;
1026
1027 while (*point)
1028 {
1029 if ((strlen (*point) > strlen (name))
1030 && (strncmp (*point, name, strlen (name)) == 0)
1031 && ((*point)[strlen (name)] == '='))
1032 {
1033 gchar *ret;
1034 ret = g_strdup (*point + strlen (name) + 1);
1035 g_strfreev (split);
1036 return ret;
1037 }
1038 point++;
1039 }
1040 g_strfreev (split);
1041 return NULL;
1042}
1043
1052gchar *
1054{
1055 return n ? n->cvss_base : NULL;
1056}
1057
1066gchar *
1068{
1069 return n ? n->dependencies : NULL;
1070}
1071
1080gchar *
1082{
1083 return n ? n->required_keys : NULL;
1084}
1085
1094gchar *
1096{
1097 return n ? n->mandatory_keys : NULL;
1098}
1099
1108gchar *
1110{
1111 return n ? n->excluded_keys : NULL;
1112}
1113
1122gchar *
1124{
1125 return n ? n->required_ports : NULL;
1126}
1127
1136gchar *
1138{
1139 return n ? n->required_udp_ports : NULL;
1140}
1141
1150gchar *
1152{
1153 return n ? n->detection : NULL;
1154}
1155
1164gchar *
1166{
1167 return n ? n->qod_type : NULL;
1168}
1169
1178gchar *
1180{
1181 return n ? n->qod : NULL;
1182}
1183
1192gchar *
1194{
1195 return n ? n->family : NULL;
1196}
1197
1205guint
1207{
1208 return n ? g_slist_length (n->prefs) : 0;
1209}
1210
1220const nvtpref_t *
1221nvti_pref (const nvti_t *n, guint p)
1222{
1223 return n ? g_slist_nth_data (n->prefs, p) : NULL;
1224}
1225
1233gint
1235{
1236 return n ? n->category : -1;
1237}
1238
1248int
1249nvti_set_oid (nvti_t *n, const gchar *oid)
1250{
1251 if (!n)
1252 return -1;
1253
1254 g_free (n->oid);
1255 n->oid = g_strdup (oid);
1256 return 0;
1257}
1258
1268int
1269nvti_set_name (nvti_t *n, const gchar *name)
1270{
1271 if (!n)
1272 return -1;
1273
1274 g_free (n->name);
1275 n->name = g_strdup (name);
1276 return 0;
1277}
1278
1288int
1289nvti_put_name (nvti_t *n, gchar *name)
1290{
1291 if (!n)
1292 return -1;
1293
1294 g_free (n->name);
1295 n->name = name;
1296 return 0;
1297}
1298
1308int
1309nvti_set_summary (nvti_t *n, const gchar *summary)
1310{
1311 if (!n)
1312 return -1;
1313
1314 g_free (n->summary);
1315 n->summary = g_strdup (summary);
1316 return 0;
1317}
1318
1328int
1329nvti_put_summary (nvti_t *n, gchar *summary)
1330{
1331 if (!n)
1332 return -1;
1333
1334 g_free (n->summary);
1335 n->summary = summary;
1336 return 0;
1337}
1338
1348int
1349nvti_set_insight (nvti_t *n, const gchar *insight)
1350{
1351 if (!n)
1352 return -1;
1353
1354 g_free (n->insight);
1355 n->insight = g_strdup (insight);
1356 return 0;
1357}
1358
1368int
1369nvti_put_insight (nvti_t *n, gchar *insight)
1370{
1371 if (!n)
1372 return -1;
1373
1374 g_free (n->insight);
1375 n->insight = insight;
1376 return 0;
1377}
1378
1388int
1389nvti_set_affected (nvti_t *n, const gchar *affected)
1390{
1391 if (!n)
1392 return -1;
1393
1394 g_free (n->affected);
1395 n->affected = g_strdup (affected);
1396 return 0;
1397}
1398
1408int
1409nvti_put_affected (nvti_t *n, gchar *affected)
1410{
1411 if (!n)
1412 return -1;
1413
1414 g_free (n->affected);
1415 n->affected = affected;
1416 return 0;
1417}
1418
1428int
1429nvti_set_impact (nvti_t *n, const gchar *impact)
1430{
1431 if (!n)
1432 return -1;
1433
1434 g_free (n->impact);
1435 n->impact = g_strdup (impact);
1436 return 0;
1437}
1438
1448int
1449nvti_put_impact (nvti_t *n, gchar *impact)
1450{
1451 if (!n)
1452 return -1;
1453
1454 g_free (n->impact);
1455 n->impact = impact;
1456 return 0;
1457}
1458
1468int
1469nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1470{
1471 if (!n)
1472 return -1;
1473
1474 n->creation_time = creation_time;
1475 return 0;
1476}
1477
1487int
1488nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1489{
1490 if (!n)
1491 return -1;
1492
1493 n->modification_time = modification_time;
1494 return 0;
1495}
1496
1506int
1507nvti_set_solution (nvti_t *n, const gchar *solution)
1508{
1509 if (!n)
1510 return -1;
1511
1512 g_free (n->solution);
1513 n->solution = g_strdup (solution);
1514 return 0;
1515}
1516
1526int
1527nvti_put_solution (nvti_t *n, gchar *solution)
1528{
1529 if (!n)
1530 return -1;
1531
1532 g_free (n->solution);
1533 n->solution = solution;
1534 return 0;
1535}
1536
1547int
1548nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1549{
1550 if (!n)
1551 return -1;
1552
1553 g_free (n->solution_type);
1554 n->solution_type = g_strdup (solution_type);
1555 return 0;
1556}
1557
1568int
1569nvti_set_solution_method (nvti_t *n, const gchar *solution_method)
1570{
1571 if (!n)
1572 return -1;
1573
1574 g_free (n->solution_method);
1575 n->solution_method = g_strdup (solution_method);
1576 return 0;
1577}
1578
1595int
1596nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1597{
1598 gchar *newvalue = NULL;
1599
1600 if (!n)
1601 return -1;
1602
1603 if (!name || !name[0])
1604 return -2;
1605
1606 if (!value || !value[0])
1607 return -3;
1608
1609 if (!strcmp (name, "last_modification"))
1610 {
1612 newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1613 }
1614 else if (!strcmp (name, "creation_date"))
1615 {
1617 newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1618 }
1619 else if (!strcmp (name, "severity_date"))
1620 newvalue = g_strdup_printf ("%i", (int) parse_nvt_timestamp (value));
1621 else if (!strcmp (name, "cvss_base"))
1622 {
1623 /* Ignore this tag because it is not being used.
1624 * It is redundant with the tag cvss_base_vector from which
1625 * it is computed.
1626 * Once GOS 6 and GVM 11 are retired, all set_tag commands
1627 * in the NASL scripts can be removed that set "cvss_base".
1628 * Once this happened this exception can be removed from the code.
1629 */
1630 return 0;
1631 }
1632
1633 if (n->tag)
1634 {
1635 gchar *newtag;
1636
1637 newtag =
1638 g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1639 g_free (n->tag);
1640 n->tag = newtag;
1641 }
1642 else
1643 n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1644
1645 g_free (newvalue);
1646
1647 return 0;
1648}
1649
1659int
1660nvti_set_tag (nvti_t *n, const gchar *tag)
1661{
1662 if (!n)
1663 return -1;
1664
1665 g_free (n->tag);
1666 if (tag && tag[0])
1667 n->tag = g_strdup (tag);
1668 else
1669 n->tag = NULL;
1670 return 0;
1671}
1672
1682int
1683nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1684{
1685 if (!n)
1686 return -1;
1687
1688 g_free (n->cvss_base);
1689 if (cvss_base && cvss_base[0])
1690 n->cvss_base = g_strdup (cvss_base);
1691 else
1692 n->cvss_base = NULL;
1693 return 0;
1694}
1695
1706int
1707nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1708{
1709 if (!n)
1710 return -1;
1711
1712 g_free (n->dependencies);
1713 if (dependencies && dependencies[0])
1714 n->dependencies = g_strdup (dependencies);
1715 else
1716 n->dependencies = NULL;
1717 return 0;
1718}
1719
1730int
1731nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1732{
1733 if (!n)
1734 return -1;
1735
1736 g_free (n->required_keys);
1737 if (required_keys && required_keys[0])
1738 n->required_keys = g_strdup (required_keys);
1739 else
1740 n->required_keys = NULL;
1741 return 0;
1742}
1743
1754int
1755nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1756{
1757 if (!n)
1758 return -1;
1759
1760 g_free (n->mandatory_keys);
1761 if (mandatory_keys && mandatory_keys[0])
1762 n->mandatory_keys = g_strdup (mandatory_keys);
1763 else
1764 n->mandatory_keys = NULL;
1765 return 0;
1766}
1767
1778int
1779nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1780{
1781 if (!n)
1782 return -1;
1783
1784 g_free (n->excluded_keys);
1785 if (excluded_keys && excluded_keys[0])
1786 n->excluded_keys = g_strdup (excluded_keys);
1787 else
1788 n->excluded_keys = NULL;
1789 return 0;
1790}
1791
1802int
1803nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1804{
1805 if (!n)
1806 return -1;
1807
1808 g_free (n->required_ports);
1809 if (required_ports && required_ports[0])
1810 n->required_ports = g_strdup (required_ports);
1811 else
1812 n->required_ports = NULL;
1813 return 0;
1814}
1815
1826int
1827nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1828{
1829 if (!n)
1830 return -1;
1831
1832 g_free (n->required_udp_ports);
1833 if (required_udp_ports && required_udp_ports[0])
1834 n->required_udp_ports = g_strdup (required_udp_ports);
1835 else
1836 n->required_udp_ports = NULL;
1837 return 0;
1838}
1839
1849int
1850nvti_set_detection (nvti_t *n, const gchar *detection)
1851{
1852 if (!n)
1853 return -1;
1854
1855 g_free (n->detection);
1856 n->detection = g_strdup (detection);
1857 return 0;
1858}
1859
1869int
1870nvti_put_detection (nvti_t *n, gchar *detection)
1871{
1872 if (!n)
1873 return -1;
1874
1875 g_free (n->detection);
1876 n->detection = detection;
1877 return 0;
1878}
1879
1890int
1891nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1892{
1893 if (!n)
1894 return -1;
1895
1896 g_free (n->qod_type);
1897 if (qod_type && qod_type[0])
1898 n->qod_type = g_strdup (qod_type);
1899 else
1900 n->qod_type = NULL;
1901 return 0;
1902}
1903
1914int
1915nvti_set_qod (nvti_t *n, const gchar *qod)
1916{
1917 if (!n)
1918 return -1;
1919
1920 g_free (n->qod);
1921 if (qod && qod[0])
1922 n->qod = g_strdup (qod);
1923 else
1924 n->qod = NULL;
1925 return 0;
1926}
1927
1937int
1938nvti_set_family (nvti_t *n, const gchar *family)
1939{
1940 if (!n)
1941 return -1;
1942
1943 g_free (n->family);
1944 n->family = g_strdup (family);
1945 return 0;
1946}
1947
1957int
1958nvti_put_family (nvti_t *n, gchar *family)
1959{
1960 if (!n)
1961 return -1;
1962
1963 g_free (n->family);
1964 n->family = family;
1965 return 0;
1966}
1967
1977int
1978nvti_set_category (nvti_t *n, const gint category)
1979{
1980 if (!n)
1981 return -1;
1982
1983 n->category = category;
1984 return 0;
1985}
1986
2002int
2003nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
2004 const gchar *ref_text)
2005{
2006 gchar **split, **item;
2007
2008 if (!n)
2009 return 1;
2010
2011 if (!ref_ids)
2012 return 2;
2013
2014 split = g_strsplit (ref_ids, ",", 0);
2015
2016 for (item = split; *item; item++)
2017 {
2018 gchar *id;
2019
2020 id = *item;
2021 g_strstrip (id);
2022
2023 if (strcmp (id, "") == 0)
2024 continue;
2025
2026 if (type)
2027 {
2028 nvti_add_vtref (n, vtref_new (type, id, ref_text));
2029 }
2030 else
2031 {
2032 gchar **split2;
2033
2034 split2 = g_strsplit (id, ":", 2);
2035 if (split2[0] && split2[1])
2036 nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
2037 g_strfreev (split2);
2038 }
2039 }
2040 g_strfreev (split);
2041
2042 return 0;
2043}
2044
2054int
2055nvti_add_required_keys (nvti_t *n, const gchar *key)
2056{
2057 gchar *old;
2058
2059 if (!n)
2060 return 1;
2061 if (!key)
2062 return 2;
2063
2064 old = n->required_keys;
2065
2066 if (old)
2067 {
2068 n->required_keys = g_strdup_printf ("%s, %s", old, key);
2069 g_free (old);
2070 }
2071 else
2072 n->required_keys = g_strdup (key);
2073
2074 return 0;
2075}
2076
2086int
2087nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
2088{
2089 gchar *old;
2090
2091 if (!n)
2092 return 1;
2093 if (!key)
2094 return 2;
2095
2096 old = n->mandatory_keys;
2097
2098 if (old)
2099 {
2100 n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
2101 g_free (old);
2102 }
2103 else
2104 n->mandatory_keys = g_strdup (key);
2105
2106 return 0;
2107}
2108
2118int
2119nvti_add_excluded_keys (nvti_t *n, const gchar *key)
2120{
2121 gchar *old;
2122
2123 if (!n)
2124 return 1;
2125 if (!key)
2126 return 2;
2127
2128 old = n->excluded_keys;
2129
2130 if (old)
2131 {
2132 n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
2133 g_free (old);
2134 }
2135 else
2136 n->excluded_keys = g_strdup (key);
2137
2138 return 0;
2139}
2140
2150int
2151nvti_add_required_ports (nvti_t *n, const gchar *port)
2152{
2153 gchar *old;
2154
2155 if (!n)
2156 return 1;
2157 if (!port)
2158 return 2;
2159
2160 old = n->required_ports;
2161
2162 if (old)
2163 {
2164 n->required_ports = g_strdup_printf ("%s, %s", old, port);
2165 g_free (old);
2166 }
2167 else
2168 n->required_ports = g_strdup (port);
2169
2170 return 0;
2171}
2172
2182int
2184{
2185 gchar *old;
2186
2187 if (!n)
2188 return 1;
2189 if (!port)
2190 return 2;
2191
2192 old = n->required_udp_ports;
2193
2194 if (old)
2195 {
2196 n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
2197 g_free (old);
2198 }
2199 else
2200 n->required_udp_ports = g_strdup (port);
2201
2202 return 0;
2203}
2204
2214int
2216{
2217 if (!n)
2218 return -1;
2219
2220 n->prefs = g_slist_append (n->prefs, np);
2221 return 0;
2222}
2223
2224/* Collections of nvtis. */
2225
2231static void
2233{
2234 nvti_free ((nvti_t *) nvti);
2235}
2236
2242nvtis_t *
2244{
2245 return g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2247}
2248
2254void
2256{
2257 if (nvtis)
2258 g_hash_table_destroy (nvtis);
2259}
2260
2267void
2269{
2270 if (nvti)
2271 g_hash_table_insert (
2272 nvtis, (gpointer) (nvti_oid (nvti) ? g_strdup (nvti_oid (nvti)) : NULL),
2273 (gpointer) nvti);
2274}
2275
2284nvti_t *
2285nvtis_lookup (nvtis_t *nvtis, const char *oid)
2286{
2287 return g_hash_table_lookup (nvtis, oid);
2288}
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:772
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:594
int nvti_put_impact(nvti_t *n, gchar *impact)
Set the impact text of a NVT, using the given memory.
Definition nvti.c:1449
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:1067
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition nvti.c:2055
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition nvti.c:1206
int nvti_set_qod(nvti_t *n, const gchar *qod)
Set the QoD of a NVT.
Definition nvti.c:1915
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition nvti.c:1151
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition nvti.c:1137
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition nvti.c:1507
gchar * nvti_get_tag(const nvti_t *n, const gchar *name)
Get a tag value by a tag name.
Definition nvti.c:1017
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition nvti.c:674
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition nvti.c:1548
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition nvti.c:1123
int nvti_put_insight(nvti_t *n, gchar *insight)
Set the insight text of a NVT, using the given memory.
Definition nvti.c:1369
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition nvti.c:801
double nvti_severity_score(const nvti_t *n)
Get the maximum severity score.
Definition nvti.c:903
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition nvti.c:566
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition nvti.c:1779
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:1891
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition nvti.c:2119
int nvti_put_detection(nvti_t *n, gchar *detection)
Set the detection text of a NVT, using the given memory.
Definition nvti.c:1870
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition nvti.c:2243
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition nvti.c:1707
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition nvti.c:1803
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1249
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition nvti.c:2183
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition nvti.c:1660
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition nvti.c:1683
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition nvti.c:1755
int nvti_put_name(nvti_t *n, gchar *name)
Set the name of a NVT, using the given memory.
Definition nvti.c:1289
int nvti_put_family(nvti_t *n, gchar *family)
Set the family of a NVT, using the given memory.
Definition nvti.c:1958
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition nvti.c:1234
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition nvti.c:1429
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:2151
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:1193
gchar * nvti_severity_vector_from_tag(const nvti_t *n)
Get the severity score.
Definition nvti.c:935
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:2232
gchar * nvti_solution_method(const nvti_t *n)
Get the solution method.
Definition nvti.c:987
guint nvti_vtseverities_len(const nvti_t *n)
Get the number of severities of the NVT.
Definition nvti.c:875
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition nvti.c:1221
gchar * nvti_qod(const nvti_t *n)
Get the QoD.
Definition nvti.c:1179
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition nvti.c:1109
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition nvti.c:1309
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition nvti.c:660
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition nvti.c:646
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:497
int nvti_put_affected(nvti_t *n, gchar *affected)
Set the affected text of a NVT, using the given memory.
Definition nvti.c:1409
int nvti_put_solution(nvti_t *n, gchar *solution)
Set the solution of a NVT, using the given memory.
Definition nvti.c:1527
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:2215
int nvti_put_summary(nvti_t *n, gchar *summary)
Set the summary of a NVT, using the given memory.
Definition nvti.c:1329
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:1349
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:1053
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition nvti.c:1731
int nvti_add_vtseverity(nvti_t *vt, vtseverity_t *s)
Add a severity to the VT Info.
Definition nvti.c:459
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition nvti.c:518
int nvti_set_solution_method(nvti_t *n, const gchar *solution_method)
Set the solution method of a NVT.
Definition nvti.c:1569
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition nvti.c:538
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1269
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2285
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition nvti.c:1165
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition nvti.c:2087
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition nvti.c:730
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition nvti.c:702
vtseverity_t * nvti_vtseverity(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition nvti.c:890
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:959
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition nvti.c:1488
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition nvti.c:1469
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition nvti.c:1081
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition nvti.c:688
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:2003
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition nvti.c:744
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:757
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:1827
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition nvti.c:1095
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition nvti.c:580
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1938
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition nvti.c:552
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:1389
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:973
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:1596
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition nvti.c:2268
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition nvti.c:716
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:2255
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition nvti.c:1850
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition nvti.c:1001
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1978
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:605
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:474
gchar * type
Preference type.
Definition nvti.c:476
gchar * dflt
Default value of the preference.
Definition nvti.c:478
int id
Preference ID.
Definition nvti.c:475
gchar * name
Name of the preference.
Definition nvti.c:477
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