Greenbone Vulnerability Management Libraries  11.0.0
nvti.c
Go to the documentation of this file.
1 /* Copyright (C) 2009-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 // One of the files of gvm-libs needs to specify the meta data
21 // for the doxygen documentation.
22 
47 /* For strptime in time.h. */
48 #undef _XOPEN_SOURCE
49 #define _XOPEN_SOURCE
50 #include "nvti.h"
51 
52 #include <stdio.h> // for sscanf
53 #include <string.h> // for strcmp
54 #include <strings.h> // for strcasecmp
55 #include <time.h> // for strptime
56 
57 #undef G_LOG_DOMAIN
58 #define G_LOG_DOMAIN "lib nvti"
59 
60 /* VT references */
61 
68 typedef struct vtref
69 {
70  gchar *type;
71  gchar *ref_id;
72  gchar *ref_text;
73 } vtref_t;
74 
88 vtref_t *
89 vtref_new (const gchar *type, const gchar *ref_id, const gchar *ref_text)
90 {
91  vtref_t *ref = g_malloc0 (sizeof (vtref_t));
92 
93  if (type)
94  ref->type = g_strdup (type);
95  if (ref_id)
96  ref->ref_id = g_strdup (ref_id);
97  if (ref_text)
98  ref->ref_text = g_strdup (ref_text);
99 
100  return (ref);
101 }
102 
108 void
110 {
111  if (!ref)
112  return;
113 
114  g_free (ref->type);
115  g_free (ref->ref_id);
116  g_free (ref->ref_text);
117  g_free (ref);
118 }
119 
128 const gchar *
129 vtref_type (const vtref_t *r)
130 {
131  return (r ? r->type : NULL);
132 }
133 
142 const gchar *
143 vtref_id (const vtref_t *r)
144 {
145  return (r ? r->ref_id : NULL);
146 }
147 
156 const gchar *
157 vtref_text (const vtref_t *r)
158 {
159  return (r ? r->ref_text : NULL);
160 }
161 
162 /* Support function for timestamps */
163 
172 static time_t
173 parse_nvt_timestamp (const gchar *str_time)
174 {
175  time_t epoch_time;
176  int offset;
177  struct tm tm;
178 
179  if ((strcmp ((char *) str_time, "") == 0)
180  || (strcmp ((char *) str_time, "$Date: $") == 0)
181  || (strcmp ((char *) str_time, "$Date$") == 0)
182  || (strcmp ((char *) str_time, "$Date:$") == 0)
183  || (strcmp ((char *) str_time, "$Date") == 0)
184  || (strcmp ((char *) str_time, "$$") == 0))
185  {
186  return 0;
187  }
188 
189  /* Parse the time. */
190 
191  /* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
192  /* $Date: 2012-02-17 16:05:26 +0100 (Fr, 17. Feb 2012) $ */
193  /* $Date: Fri, 11 Nov 2011 14:42:28 +0100 $ */
194  memset (&tm, 0, sizeof (struct tm));
195  if (strptime ((char *) str_time, "%F %T %z", &tm) == NULL)
196  {
197  memset (&tm, 0, sizeof (struct tm));
198  if (strptime ((char *) str_time, "$Date: %F %T %z", &tm) == NULL)
199  {
200  memset (&tm, 0, sizeof (struct tm));
201  if (strptime ((char *) str_time, "%a %b %d %T %Y %z", &tm) == NULL)
202  {
203  memset (&tm, 0, sizeof (struct tm));
204  if (strptime ((char *) str_time, "$Date: %a, %d %b %Y %T %z", &tm)
205  == NULL)
206  {
207  memset (&tm, 0, sizeof (struct tm));
208  if (strptime ((char *) str_time, "$Date: %a %b %d %T %Y %z",
209  &tm)
210  == NULL)
211  {
212  g_warning ("%s: Failed to parse time: %s", __FUNCTION__,
213  str_time);
214  return 0;
215  }
216  }
217  }
218  }
219  }
220  epoch_time = mktime (&tm);
221  if (epoch_time == -1)
222  {
223  g_warning ("%s: Failed to make time: %s", __FUNCTION__, str_time);
224  return 0;
225  }
226 
227  /* Get the timezone offset from the str_time. */
228 
229  if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
230  != 1)
231  && (sscanf ((char *) str_time, "$Date: %*u-%*u-%*u %*u:%*u:%*u %d%*[^]]",
232  &offset)
233  != 1)
234  && (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
235  &offset)
236  != 1)
237  && (sscanf ((char *) str_time,
238  "$Date: %*s %*s %*s %*u %*u:%*u:%*u %d%*[^]]", &offset)
239  != 1)
240  && (sscanf ((char *) str_time,
241  "$Date: %*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]", &offset)
242  != 1))
243  {
244  g_warning ("%s: Failed to parse timezone offset: %s", __FUNCTION__,
245  str_time);
246  return 0;
247  }
248 
249  /* Use the offset to convert to UTC. */
250 
251  if (offset < 0)
252  {
253  epoch_time += ((-offset) / 100) * 60 * 60;
254  epoch_time += ((-offset) % 100) * 60;
255  }
256  else if (offset > 0)
257  {
258  epoch_time -= (offset / 100) * 60 * 60;
259  epoch_time -= (offset % 100) * 60;
260  }
261 
262  return epoch_time;
263 }
264 
265 /* VT Information */
266 
270 typedef struct nvti
271 {
272  gchar *oid;
273  gchar *name;
275  gchar *summary;
276  gchar *insight;
277  gchar *affected;
278  gchar *impact;
280  time_t creation_time;
283  gchar *solution;
284  gchar *solution_type;
286  gchar *tag;
287  gchar *cvss_base;
289  gchar *dependencies;
290  gchar *required_keys;
291  gchar *mandatory_keys;
292  gchar *excluded_keys;
293  gchar *required_ports;
294  gchar
297  gchar *detection;
298  gchar *qod_type;
300  GSList *refs;
301  GSList *prefs;
303  // The following are not settled yet.
304  gint timeout;
305  gint category;
306  gchar *family;
307 } nvti_t;
308 
318 int
320 {
321  if (!vt)
322  return (-1);
323 
324  vt->refs = g_slist_append (vt->refs, ref);
325  return (0);
326 }
327 
328 /* VT preferences */
329 
333 typedef struct nvtpref
334 {
335  int id;
336  gchar *type;
337  gchar *name;
338  gchar *dflt;
339 } nvtpref_t;
340 
356 nvtpref_t *
357 nvtpref_new (int id, gchar *name, gchar *type, gchar *dflt)
358 {
359  nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
360 
361  np->id = id;
362  if (name)
363  np->name = g_strdup (name);
364  if (type)
365  np->type = g_strdup (type);
366  if (dflt)
367  np->dflt = g_strdup (dflt);
368 
369  return (np);
370 }
371 
377 void
379 {
380  if (!np)
381  return;
382 
383  g_free (np->name);
384  g_free (np->type);
385  g_free (np->dflt);
386  g_free (np);
387 }
388 
397 int
399 {
400  return np ? np->id : -1;
401 }
402 
411 gchar *
413 {
414  return (np ? np->name : NULL);
415 }
416 
425 gchar *
427 {
428  return (np ? np->type : NULL);
429 }
430 
439 gchar *
441 {
442  return (np ? np->dflt : NULL);
443 }
444 
453 nvti_t *
454 nvti_new (void)
455 {
456  return ((nvti_t *) g_malloc0 (sizeof (nvti_t)));
457 }
458 
464 void
466 {
467  if (!n)
468  return;
469 
470  g_free (n->oid);
471  g_free (n->name);
472  g_free (n->summary);
473  g_free (n->insight);
474  g_free (n->affected);
475  g_free (n->impact);
476  g_free (n->solution);
477  g_free (n->solution_type);
478  g_free (n->tag);
479  g_free (n->cvss_base);
480  g_free (n->dependencies);
481  g_free (n->required_keys);
482  g_free (n->mandatory_keys);
483  g_free (n->excluded_keys);
484  g_free (n->required_ports);
485  g_free (n->required_udp_ports);
486  g_free (n->detection);
487  g_free (n->qod_type);
488  g_free (n->family);
489  g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
490  g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
491  g_free (n);
492 }
493 
502 gchar *
503 nvti_oid (const nvti_t *n)
504 {
505  return (n ? n->oid : NULL);
506 }
507 
516 gchar *
517 nvti_name (const nvti_t *n)
518 {
519  return (n ? n->name : NULL);
520 }
521 
530 gchar *
532 {
533  return (n ? n->summary : NULL);
534 }
535 
544 gchar *
546 {
547  return (n ? n->insight : NULL);
548 }
549 
558 gchar *
560 {
561  return (n ? n->affected : NULL);
562 }
563 
572 gchar *
573 nvti_impact (const nvti_t *n)
574 {
575  return (n ? n->impact : NULL);
576 }
577 
586 time_t
588 {
589  return (n ? n->creation_time : 0);
590 }
591 
600 time_t
602 {
603  return (n ? n->modification_time : 0);
604 }
605 
613 guint
615 {
616  return (n ? g_slist_length (n->refs) : 0);
617 }
618 
628 vtref_t *
629 nvti_vtref (const nvti_t *n, guint p)
630 {
631  return (n ? g_slist_nth_data (n->refs, p) : NULL);
632 }
633 
657 gchar *
658 nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
659  guint use_types)
660 {
661  gchar *refs, *refs2, **exclude_item;
662  vtref_t *ref;
663  guint i, exclude;
664  gchar **exclude_split;
665 
666  if (!n)
667  return (NULL);
668 
669  refs = NULL;
670  refs2 = NULL;
671  exclude = 0;
672 
673  if (exclude_types && exclude_types[0])
674  exclude_split = g_strsplit (exclude_types, ",", 0);
675  else
676  exclude_split = NULL;
677 
678  for (i = 0; i < g_slist_length (n->refs); i++)
679  {
680  ref = g_slist_nth_data (n->refs, i);
681  if (type && strcasecmp (ref->type, type) != 0)
682  continue;
683 
684  if (exclude_split)
685  {
686  exclude = 0;
687  for (exclude_item = exclude_split; *exclude_item; exclude_item++)
688  {
689  if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
690  {
691  exclude = 1;
692  break;
693  }
694  }
695  }
696 
697  if (!exclude)
698  {
699  if (use_types)
700  {
701  if (refs)
702  refs2 =
703  g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
704  else
705  refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
706  }
707  else
708  {
709  if (refs)
710  refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
711  else
712  refs2 = g_strdup_printf ("%s", ref->ref_id);
713  }
714  g_free (refs);
715  refs = refs2;
716  }
717  }
718 
719  g_strfreev (exclude_split);
720 
721  return (refs);
722 }
723 
732 gchar *
734 {
735  return (n ? n->solution : NULL);
736 }
737 
746 gchar *
748 {
749  return (n ? n->solution_type : NULL);
750 }
751 
760 gchar *
761 nvti_tag (const nvti_t *n)
762 {
763  return (n ? n->tag : NULL);
764 }
765 
774 gchar *
776 {
777  return (n ? n->cvss_base : NULL);
778 }
779 
788 gchar *
790 {
791  return (n ? n->dependencies : NULL);
792 }
793 
802 gchar *
804 {
805  return (n ? n->required_keys : NULL);
806 }
807 
816 gchar *
818 {
819  return (n ? n->mandatory_keys : NULL);
820 }
821 
830 gchar *
832 {
833  return (n ? n->excluded_keys : NULL);
834 }
835 
844 gchar *
846 {
847  return (n ? n->required_ports : NULL);
848 }
849 
858 gchar *
860 {
861  return (n ? n->required_udp_ports : NULL);
862 }
863 
872 gchar *
874 {
875  return (n ? n->detection : NULL);
876 }
877 
886 gchar *
888 {
889  return (n ? n->qod_type : NULL);
890 }
891 
900 gchar *
901 nvti_family (const nvti_t *n)
902 {
903  return (n ? n->family : NULL);
904 }
905 
913 guint
915 {
916  return (n ? g_slist_length (n->prefs) : 0);
917 }
918 
928 const nvtpref_t *
929 nvti_pref (const nvti_t *n, guint p)
930 {
931  return (n ? g_slist_nth_data (n->prefs, p) : NULL);
932 }
933 
942 gint
944 {
945  return (n ? n->timeout : -1);
946 }
947 
955 gint
957 {
958  return (n ? n->category : -1);
959 }
960 
970 int
971 nvti_set_oid (nvti_t *n, const gchar *oid)
972 {
973  if (!n)
974  return (-1);
975 
976  if (n->oid)
977  g_free (n->oid);
978  n->oid = g_strdup (oid);
979  return (0);
980 }
981 
991 int
992 nvti_set_name (nvti_t *n, const gchar *name)
993 {
994  if (!n)
995  return (-1);
996 
997  if (n->name)
998  g_free (n->name);
999  n->name = g_strdup (name);
1000  return (0);
1001 }
1002 
1012 int
1013 nvti_set_summary (nvti_t *n, const gchar *summary)
1014 {
1015  if (!n)
1016  return (-1);
1017 
1018  if (n->summary)
1019  g_free (n->summary);
1020  n->summary = g_strdup (summary);
1021  return (0);
1022 }
1023 
1033 int
1034 nvti_set_insight (nvti_t *n, const gchar *insight)
1035 {
1036  if (!n)
1037  return (-1);
1038 
1039  if (n->insight)
1040  g_free (n->insight);
1041  n->insight = g_strdup (insight);
1042  return (0);
1043 }
1044 
1054 int
1055 nvti_set_affected (nvti_t *n, const gchar *affected)
1056 {
1057  if (!n)
1058  return (-1);
1059 
1060  if (n->affected)
1061  g_free (n->affected);
1062  n->affected = g_strdup (affected);
1063  return (0);
1064 }
1065 
1075 int
1076 nvti_set_impact (nvti_t *n, const gchar *impact)
1077 {
1078  if (!n)
1079  return (-1);
1080 
1081  if (n->impact)
1082  g_free (n->impact);
1083  n->impact = g_strdup (impact);
1084  return (0);
1085 }
1086 
1096 int
1097 nvti_set_creation_time (nvti_t *n, const time_t creation_time)
1098 {
1099  if (!n)
1100  return (-1);
1101 
1102  n->creation_time = creation_time;
1103  return (0);
1104 }
1105 
1115 int
1116 nvti_set_modification_time (nvti_t *n, const time_t modification_time)
1117 {
1118  if (!n)
1119  return (-1);
1120 
1121  n->modification_time = modification_time;
1122  return (0);
1123 }
1124 
1134 int
1135 nvti_set_solution (nvti_t *n, const gchar *solution)
1136 {
1137  if (!n)
1138  return (-1);
1139 
1140  if (n->solution)
1141  g_free (n->solution);
1142  n->solution = g_strdup (solution);
1143  return (0);
1144 }
1145 
1156 int
1157 nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
1158 {
1159  if (!n)
1160  return (-1);
1161 
1162  if (n->solution_type)
1163  g_free (n->solution_type);
1164  n->solution_type = g_strdup (solution_type);
1165  return (0);
1166 }
1167 
1184 int
1185 nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
1186 {
1187  gchar *newvalue = NULL;
1188 
1189  if (!n)
1190  return (-1);
1191 
1192  if (!name || !name[0])
1193  return (-2);
1194 
1195  if (!value || !value[0])
1196  return (-3);
1197 
1198  if (!strcmp (name, "last_modification"))
1199  {
1201  newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
1202  }
1203  else if (!strcmp (name, "creation_date"))
1204  {
1206  newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
1207  }
1208  else if (!strcmp (name, "cvss_base"))
1209  {
1210  /* Ignore this tag because it is not being used.
1211  * It is redundant with the tag cvss_base_vector from which
1212  * it is computed.
1213  * Once GOS 6 and GVM 11 are retired, all set_tag commands
1214  * in the NASL scripts can be removed that set "cvss_base".
1215  * Once this happened this exception can be removed from the code.
1216  */
1217  return (0);
1218  }
1219 
1220  if (n->tag)
1221  {
1222  gchar *newtag;
1223 
1224  newtag =
1225  g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
1226  g_free (n->tag);
1227  n->tag = newtag;
1228  }
1229  else
1230  n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
1231 
1232  if (newvalue)
1233  g_free (newvalue);
1234 
1235  return (0);
1236 }
1237 
1247 int
1248 nvti_set_tag (nvti_t *n, const gchar *tag)
1249 {
1250  if (!n)
1251  return (-1);
1252 
1253  if (n->tag)
1254  g_free (n->tag);
1255  if (tag && tag[0])
1256  n->tag = g_strdup (tag);
1257  else
1258  n->tag = NULL;
1259  return (0);
1260 }
1261 
1271 int
1272 nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
1273 {
1274  if (!n)
1275  return (-1);
1276 
1277  if (n->cvss_base)
1278  g_free (n->cvss_base);
1279  if (cvss_base && cvss_base[0])
1280  n->cvss_base = g_strdup (cvss_base);
1281  else
1282  n->cvss_base = NULL;
1283  return (0);
1284 }
1285 
1296 int
1297 nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
1298 {
1299  if (!n)
1300  return (-1);
1301 
1302  if (n->dependencies)
1303  g_free (n->dependencies);
1304  if (dependencies && dependencies[0])
1305  n->dependencies = g_strdup (dependencies);
1306  else
1307  n->dependencies = NULL;
1308  return (0);
1309 }
1310 
1321 int
1322 nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
1323 {
1324  if (!n)
1325  return (-1);
1326 
1327  if (n->required_keys)
1328  g_free (n->required_keys);
1329  if (required_keys && required_keys[0])
1330  n->required_keys = g_strdup (required_keys);
1331  else
1332  n->required_keys = NULL;
1333  return (0);
1334 }
1335 
1346 int
1347 nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
1348 {
1349  if (!n)
1350  return (-1);
1351 
1352  if (n->mandatory_keys)
1353  g_free (n->mandatory_keys);
1354  if (mandatory_keys && mandatory_keys[0])
1355  n->mandatory_keys = g_strdup (mandatory_keys);
1356  else
1357  n->mandatory_keys = NULL;
1358  return (0);
1359 }
1360 
1371 int
1372 nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
1373 {
1374  if (!n)
1375  return (-1);
1376 
1377  if (n->excluded_keys)
1378  g_free (n->excluded_keys);
1379  if (excluded_keys && excluded_keys[0])
1380  n->excluded_keys = g_strdup (excluded_keys);
1381  else
1382  n->excluded_keys = NULL;
1383  return (0);
1384 }
1385 
1396 int
1397 nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
1398 {
1399  if (!n)
1400  return (-1);
1401 
1402  if (n->required_ports)
1403  g_free (n->required_ports);
1404  if (required_ports && required_ports[0])
1405  n->required_ports = g_strdup (required_ports);
1406  else
1407  n->required_ports = NULL;
1408  return (0);
1409 }
1410 
1421 int
1422 nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
1423 {
1424  if (!n)
1425  return (-1);
1426 
1427  if (n->required_udp_ports)
1428  g_free (n->required_udp_ports);
1429  if (required_udp_ports && required_udp_ports[0])
1430  n->required_udp_ports = g_strdup (required_udp_ports);
1431  else
1432  n->required_udp_ports = NULL;
1433  return (0);
1434 }
1435 
1445 int
1446 nvti_set_detection (nvti_t *n, const gchar *detection)
1447 {
1448  if (!n)
1449  return (-1);
1450 
1451  if (n->detection)
1452  g_free (n->detection);
1453  n->detection = g_strdup (detection);
1454  return (0);
1455 }
1456 
1467 int
1468 nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
1469 {
1470  if (!n)
1471  return (-1);
1472 
1473  if (n->qod_type)
1474  g_free (n->qod_type);
1475  if (qod_type && qod_type[0])
1476  n->qod_type = g_strdup (qod_type);
1477  else
1478  n->qod_type = NULL;
1479  return (0);
1480 }
1481 
1491 int
1492 nvti_set_family (nvti_t *n, const gchar *family)
1493 {
1494  if (!n)
1495  return (-1);
1496 
1497  if (n->family)
1498  g_free (n->family);
1499  n->family = g_strdup (family);
1500  return (0);
1501 }
1502 
1512 int
1513 nvti_set_timeout (nvti_t *n, const gint timeout)
1514 {
1515  if (!n)
1516  return (-1);
1517 
1518  n->timeout = timeout;
1519  return (0);
1520 }
1521 
1531 int
1532 nvti_set_category (nvti_t *n, const gint category)
1533 {
1534  if (!n)
1535  return (-1);
1536 
1537  n->category = category;
1538  return (0);
1539 }
1540 
1556 int
1557 nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
1558  const gchar *ref_text)
1559 {
1560  gchar **split, **item;
1561 
1562  if (!n)
1563  return (1);
1564 
1565  if (!ref_ids)
1566  return (2);
1567 
1568  split = g_strsplit (ref_ids, ",", 0);
1569 
1570  for (item = split; *item; item++)
1571  {
1572  gchar *id;
1573 
1574  id = *item;
1575  g_strstrip (id);
1576 
1577  if (strcmp (id, "") == 0)
1578  continue;
1579 
1580  if (type)
1581  {
1582  nvti_add_vtref (n, vtref_new (type, id, ref_text));
1583  }
1584  else
1585  {
1586  gchar **split2;
1587 
1588  split2 = g_strsplit (id, ":", 2);
1589  if (split2[0] && split2[1])
1590  nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
1591  g_strfreev (split2);
1592  }
1593  }
1594  g_strfreev (split);
1595 
1596  return (0);
1597 }
1598 
1608 int
1609 nvti_add_required_keys (nvti_t *n, const gchar *key)
1610 {
1611  gchar *old;
1612 
1613  if (!n)
1614  return (1);
1615  if (!key)
1616  return (2);
1617 
1618  old = n->required_keys;
1619 
1620  if (old)
1621  {
1622  n->required_keys = g_strdup_printf ("%s, %s", old, key);
1623  g_free (old);
1624  }
1625  else
1626  n->required_keys = g_strdup (key);
1627 
1628  return (0);
1629 }
1630 
1640 int
1641 nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
1642 {
1643  gchar *old;
1644 
1645  if (!n)
1646  return (1);
1647  if (!key)
1648  return (2);
1649 
1650  old = n->mandatory_keys;
1651 
1652  if (old)
1653  {
1654  n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
1655  g_free (old);
1656  }
1657  else
1658  n->mandatory_keys = g_strdup (key);
1659 
1660  return (0);
1661 }
1662 
1672 int
1673 nvti_add_excluded_keys (nvti_t *n, const gchar *key)
1674 {
1675  gchar *old;
1676 
1677  if (!n)
1678  return (1);
1679  if (!key)
1680  return (2);
1681 
1682  old = n->excluded_keys;
1683 
1684  if (old)
1685  {
1686  n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
1687  g_free (old);
1688  }
1689  else
1690  n->excluded_keys = g_strdup (key);
1691 
1692  return (0);
1693 }
1694 
1704 int
1705 nvti_add_required_ports (nvti_t *n, const gchar *port)
1706 {
1707  gchar *old;
1708 
1709  if (!n)
1710  return (1);
1711  if (!port)
1712  return (2);
1713 
1714  old = n->required_ports;
1715 
1716  if (old)
1717  {
1718  n->required_ports = g_strdup_printf ("%s, %s", old, port);
1719  g_free (old);
1720  }
1721  else
1722  n->required_ports = g_strdup (port);
1723 
1724  return (0);
1725 }
1726 
1736 int
1737 nvti_add_required_udp_ports (nvti_t *n, const gchar *port)
1738 {
1739  gchar *old;
1740 
1741  if (!n)
1742  return (1);
1743  if (!port)
1744  return (2);
1745 
1746  old = n->required_udp_ports;
1747 
1748  if (old)
1749  {
1750  n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
1751  g_free (old);
1752  }
1753  else
1754  n->required_udp_ports = g_strdup (port);
1755 
1756  return (0);
1757 }
1758 
1768 int
1770 {
1771  if (!n)
1772  return (-1);
1773 
1774  n->prefs = g_slist_append (n->prefs, np);
1775  return (0);
1776 }
1777 
1778 /* Collections of nvtis. */
1779 
1785 static void
1787 {
1788  nvti_free ((nvti_t *) nvti);
1789 }
1790 
1796 nvtis_t *
1798 {
1799  return g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1801 }
1802 
1808 void
1810 {
1811  if (nvtis)
1812  g_hash_table_destroy (nvtis);
1813 }
1814 
1821 void
1823 {
1824  if (nvti)
1825  g_hash_table_insert (nvtis, (gpointer) nvti_oid (nvti), (gpointer) nvti);
1826 }
1827 
1836 nvti_t *
1837 nvtis_lookup (nvtis_t *nvtis, const char *oid)
1838 {
1839  return g_hash_table_lookup (nvtis, oid);
1840 }
nvti_solution_type
gchar * nvti_solution_type(const nvti_t *n)
Get the solution type.
Definition: nvti.c:747
vtref_text
const gchar * vtref_text(const vtref_t *r)
Get the text of a reference.
Definition: nvti.c:157
nvti_pref
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition: nvti.c:929
nvti_add_vtref
int nvti_add_vtref(nvti_t *vt, vtref_t *ref)
Add a reference to the VT Info.
Definition: nvti.c:319
nvti::mandatory_keys
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition: nvti.c:291
nvti::insight
gchar * insight
The insight.
Definition: nvti.c:276
nvti_set_required_udp_ports
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:1422
vtref::ref_text
gchar * ref_text
Optional additional text.
Definition: nvti.c:72
nvti::required_keys
gchar * required_keys
List of required KB keys of this NVT.
Definition: nvti.c:290
nvtis_free
void nvtis_free(nvtis_t *nvtis)
Free a collection of NVT Infos.
Definition: nvti.c:1809
nvti_cvss_base
gchar * nvti_cvss_base(const nvti_t *n)
Get the CVSS base.
Definition: nvti.c:775
nvti_t
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
nvtis_t
GHashTable nvtis_t
A collection of information records corresponding to NVTs.
Definition: nvti.h:207
nvti::solution_type
gchar * solution_type
The solution type.
Definition: nvti.c:284
nvti_set_creation_time
int nvti_set_creation_time(nvti_t *n, const time_t creation_time)
Set the creation time of a NVT.
Definition: nvti.c:1097
nvti_set_impact
int nvti_set_impact(nvti_t *n, const gchar *impact)
Set the impact text of a NVT.
Definition: nvti.c:1076
nvti_vtref_len
guint nvti_vtref_len(const nvti_t *n)
Get the number of references of the NVT.
Definition: nvti.c:614
nvti_set_required_keys
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition: nvti.c:1322
nvti_family
gchar * nvti_family(const nvti_t *n)
Get the family name.
Definition: nvti.c:901
nvtpref_t
struct nvtpref nvtpref_t
The structure for a preference of a NVT.
vtref
The structure for a cross reference of a VT.
Definition: nvti.c:68
nvti_insight
gchar * nvti_insight(const nvti_t *n)
Get the text about insight.
Definition: nvti.c:545
nvti_add_required_ports
int nvti_add_required_ports(nvti_t *n, const gchar *port)
Add a required port of a NVT.
Definition: nvti.c:1705
nvti::detection
gchar * detection
Detection description.
Definition: nvti.c:297
nvti_detection
gchar * nvti_detection(const nvti_t *n)
Get the text about detection.
Definition: nvti.c:873
nvti_oid
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:503
nvti_set_insight
int nvti_set_insight(nvti_t *n, const gchar *insight)
Set the insight text of a NVT.
Definition: nvti.c:1034
nvti::name
gchar * name
The name.
Definition: nvti.c:273
nvti_add_required_keys
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition: nvti.c:1609
nvti_add_pref
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition: nvti.c:1769
nvti::solution
gchar * solution
The solution.
Definition: nvti.c:283
nvtis_add
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:1822
nvti_set_cvss_base
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition: nvti.c:1272
nvtpref::dflt
gchar * dflt
Default value of the preference.
Definition: nvti.c:338
vtref::type
gchar * type
Reference type ("cve", "bid", ...)
Definition: nvti.c:70
nvtpref_id
int nvtpref_id(const nvtpref_t *np)
Get the ID of a NVT Preference.
Definition: nvti.c:398
nvti_set_name
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition: nvti.c:992
nvti::timeout
gint timeout
Default timeout time for this NVT.
Definition: nvti.c:304
nvti::creation_time
time_t creation_time
Time of creation, seconds since epoch.
Definition: nvti.c:280
nvti::cvss_base
gchar * cvss_base
CVSS base score for this NVT.
Definition: nvti.c:287
nvti_set_solution
int nvti_set_solution(nvti_t *n, const gchar *solution)
Set the solution of a NVT.
Definition: nvti.c:1135
nvti::affected
gchar * affected
Affected systems.
Definition: nvti.c:277
nvti::required_udp_ports
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition: nvti.c:295
nvti::category
gint category
The category, this NVT belongs to.
Definition: nvti.c:305
nvti_refs
gchar * nvti_refs(const nvti_t *n, const gchar *type, const gchar *exclude_types, guint use_types)
Get references as string.
Definition: nvti.c:658
nvti_required_udp_ports
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition: nvti.c:859
nvti
The structure of a information record that corresponds to a NVT.
Definition: nvti.c:270
nvti_required_ports
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition: nvti.c:845
nvti_modification_time
time_t nvti_modification_time(const nvti_t *n)
Get the modification time.
Definition: nvti.c:601
nvti_category
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition: nvti.c:956
vtref_free
void vtref_free(vtref_t *ref)
Free memory of a vtref structure.
Definition: nvti.c:109
nvti_set_excluded_keys
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition: nvti.c:1372
nvtpref::name
gchar * name
Name of the preference.
Definition: nvti.c:337
nvti_set_qod_type
int nvti_set_qod_type(nvti_t *n, const gchar *qod_type)
Set the QoD type of a NVT.
Definition: nvti.c:1468
nvti::oid
gchar * oid
Object ID.
Definition: nvti.c:272
nvti::excluded_keys
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition: nvti.c:292
nvti_add_refs
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:1557
nvti_solution
gchar * nvti_solution(const nvti_t *n)
Get the solution.
Definition: nvti.c:733
nvti_free
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition: nvti.c:465
nvtis_new
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition: nvti.c:1797
nvti_impact
gchar * nvti_impact(const nvti_t *n)
Get the text about impact.
Definition: nvti.c:573
nvti_set_modification_time
int nvti_set_modification_time(nvti_t *n, const time_t modification_time)
Set the modification time of a NVT.
Definition: nvti.c:1116
nvtpref
The structure for a preference of a NVT.
Definition: nvti.c:333
vtref_t
struct vtref vtref_t
The structure for a cross reference of a VT.
nvti_set_timeout
int nvti_set_timeout(nvti_t *n, const gint timeout)
Set the timeout of a NVT Info.
Definition: nvti.c:1513
nvti_pref_len
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition: nvti.c:914
nvtpref_free
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition: nvti.c:378
nvti_set_dependencies
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition: nvti.c:1297
nvti_add_tag
int nvti_add_tag(nvti_t *n, const gchar *name, const gchar *value)
Add a tag to the NVT tags. The tag names "last_modification" and "creation_date" are treated special:...
Definition: nvti.c:1185
nvtpref::type
gchar * type
Preference type.
Definition: nvti.c:336
nvti::impact
gchar * impact
Impact of vulnerability.
Definition: nvti.c:278
vtref_new
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:89
nvtis_lookup
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:1837
free_nvti_for_hash_table
static void free_nvti_for_hash_table(gpointer nvti)
Free an NVT Info, for g_hash_table_destroy.
Definition: nvti.c:1786
nvti_tag
gchar * nvti_tag(const nvti_t *n)
Get the tags.
Definition: nvti.c:761
nvti_vtref
vtref_t * nvti_vtref(const nvti_t *n, guint p)
Get the n'th reference of the NVT.
Definition: nvti.c:629
nvti::summary
gchar * summary
The summary.
Definition: nvti.c:275
strings.h
String utilities.
nvti_set_tag
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:1248
nvti_add_required_udp_ports
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition: nvti.c:1737
nvtpref_new
nvtpref_t * nvtpref_new(int id, gchar *name, gchar *type, gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition: nvti.c:357
nvti::family
gchar * family
Family the NVT belongs to.
Definition: nvti.c:306
nvtpref::id
int id
Preference ID.
Definition: nvti.c:335
nvti::modification_time
time_t modification_time
Time of last change, sec. since epoch.
Definition: nvti.c:281
nvti_affected
gchar * nvti_affected(const nvti_t *n)
Get the text about affected systems.
Definition: nvti.c:559
nvtpref_name
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition: nvti.c:412
nvti_mandatory_keys
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition: nvti.c:817
nvti_timeout
gint nvti_timeout(const nvti_t *n)
Get the timeout for this NVT.
Definition: nvti.c:943
nvti_dependencies
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition: nvti.c:789
nvti_add_mandatory_keys
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition: nvti.c:1641
nvti::dependencies
gchar * dependencies
List of dependencies of this NVT.
Definition: nvti.c:289
nvti_name
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition: nvti.c:517
nvti::tag
gchar * tag
List of tags attached to this NVT.
Definition: nvti.c:286
nvti_excluded_keys
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition: nvti.c:831
nvti::required_ports
gchar * required_ports
List of required ports of this NVT.
Definition: nvti.c:293
nvti_set_required_ports
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition: nvti.c:1397
nvti_qod_type
gchar * nvti_qod_type(const nvti_t *n)
Get the QoD type.
Definition: nvti.c:887
nvti_set_summary
int nvti_set_summary(nvti_t *n, const gchar *summary)
Set the summary of a NVT.
Definition: nvti.c:1013
nvti::qod_type
gchar * qod_type
Quality of detection type.
Definition: nvti.c:298
nvti_set_solution_type
int nvti_set_solution_type(nvti_t *n, const gchar *solution_type)
Set the solution type of a NVT.
Definition: nvti.c:1157
nvti_set_detection
int nvti_set_detection(nvti_t *n, const gchar *detection)
Set the detection text of a NVT.
Definition: nvti.c:1446
nvtpref_default
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition: nvti.c:440
nvti_creation_time
time_t nvti_creation_time(const nvti_t *n)
Get the creation time.
Definition: nvti.c:587
nvti_new
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:454
nvti_set_affected
int nvti_set_affected(nvti_t *n, const gchar *affected)
Set the affected text of a NVT.
Definition: nvti.c:1055
nvtpref_type
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition: nvti.c:426
vtref_type
const gchar * vtref_type(const vtref_t *r)
Get the type of a reference.
Definition: nvti.c:129
nvti_set_family
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition: nvti.c:1492
nvti_set_category
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition: nvti.c:1532
parse_nvt_timestamp
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:173
nvti_set_oid
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:971
nvti_set_mandatory_keys
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition: nvti.c:1347
nvti_summary
gchar * nvti_summary(const nvti_t *n)
Get the summary.
Definition: nvti.c:531
nvti::prefs
GSList * prefs
Collection of NVT preferences.
Definition: nvti.c:301
nvti.h
Protos and data structures for NVT Information data sets.
nvti::refs
GSList * refs
Collection of VT references.
Definition: nvti.c:300
nvti_add_excluded_keys
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition: nvti.c:1673
vtref_id
const gchar * vtref_id(const vtref_t *r)
Get the id of a reference.
Definition: nvti.c:143
vtref::ref_id
gchar * ref_id
Actual reference ID ("CVE-2018-1234", etc)
Definition: nvti.c:71
nvti_required_keys
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition: nvti.c:803