Greenbone Vulnerability Management Libraries  10.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 #include "nvti.h"
48 
49 #include <stdio.h>
50 
51 #undef G_LOG_DOMAIN
52 #define G_LOG_DOMAIN "lib nvti"
53 
67 nvtpref_t *
68 nvtpref_new (gchar *name, gchar *type, gchar *dflt)
69 {
70  nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
71 
72  if (!np)
73  return NULL;
74 
75  if (name)
76  np->name = g_strdup (name);
77  if (type)
78  np->type = g_strdup (type);
79  if (dflt)
80  np->dflt = g_strdup (dflt);
81 
82  return (np);
83 }
84 
90 void
92 {
93  if (!np)
94  return;
95 
96  if (np->name)
97  g_free (np->name);
98  if (np->type)
99  g_free (np->type);
100  if (np->dflt)
101  g_free (np->dflt);
102  g_free (np);
103 }
104 
113 gchar *
115 {
116  return (np ? np->name : NULL);
117 }
118 
127 gchar *
129 {
130  return (np ? np->type : NULL);
131 }
132 
141 gchar *
143 {
144  return (np ? np->dflt : NULL);
145 }
146 
155 nvti_t *
156 nvti_new (void)
157 {
158  return ((nvti_t *) g_malloc0 (sizeof (nvti_t)));
159 }
160 
166 void
168 {
169  if (!n)
170  return;
171 
172  if (n->oid)
173  g_free (n->oid);
174  if (n->name)
175  g_free (n->name);
176  if (n->cve)
177  g_free (n->cve);
178  if (n->bid)
179  g_free (n->bid);
180  if (n->xref)
181  g_free (n->xref);
182  if (n->tag)
183  g_free (n->tag);
184  if (n->cvss_base)
185  g_free (n->cvss_base);
186  if (n->dependencies)
187  g_free (n->dependencies);
188  if (n->required_keys)
189  g_free (n->required_keys);
190  if (n->mandatory_keys)
191  g_free (n->mandatory_keys);
192  if (n->excluded_keys)
193  g_free (n->excluded_keys);
194  if (n->required_ports)
195  g_free (n->required_ports);
196  if (n->required_udp_ports)
197  g_free (n->required_udp_ports);
198  if (n->family)
199  g_free (n->family);
200  if (n->prefs)
201  {
202  int i, len = g_slist_length (n->prefs);
203  for (i = 0; i < len; i++)
204  nvtpref_free (g_slist_nth_data (n->prefs, i));
205  g_slist_free (n->prefs);
206  }
207  g_free (n);
208 }
209 
218 gchar *
219 nvti_oid (const nvti_t *n)
220 {
221  return (n ? n->oid : NULL);
222 }
223 
232 gchar *
233 nvti_name (const nvti_t *n)
234 {
235  return (n ? n->name : NULL);
236 }
237 
246 gchar *
247 nvti_cve (const nvti_t *n)
248 {
249  return (n ? n->cve : NULL);
250 }
251 
260 gchar *
261 nvti_bid (const nvti_t *n)
262 {
263  return (n ? n->bid : NULL);
264 }
265 
274 gchar *
275 nvti_xref (const nvti_t *n)
276 {
277  return (n ? n->xref : NULL);
278 }
279 
288 gchar *
289 nvti_tag (const nvti_t *n)
290 {
291  return (n ? n->tag : NULL);
292 }
293 
302 gchar *
304 {
305  return (n ? n->cvss_base : NULL);
306 }
307 
316 gchar *
318 {
319  return (n ? n->dependencies : NULL);
320 }
321 
330 gchar *
332 {
333  return (n ? n->required_keys : NULL);
334 }
335 
344 gchar *
346 {
347  return (n ? n->mandatory_keys : NULL);
348 }
349 
358 gchar *
360 {
361  return (n ? n->excluded_keys : NULL);
362 }
363 
372 gchar *
374 {
375  return (n ? n->required_ports : NULL);
376 }
377 
386 gchar *
388 {
389  return (n ? n->required_udp_ports : NULL);
390 }
391 
400 gchar *
401 nvti_family (const nvti_t *n)
402 {
403  return (n ? n->family : NULL);
404 }
405 
413 guint
415 {
416  return (n ? g_slist_length (n->prefs) : 0);
417 }
418 
428 const nvtpref_t *
429 nvti_pref (const nvti_t *n, guint p)
430 {
431  return (n ? g_slist_nth_data (n->prefs, p) : NULL);
432 }
433 
442 gint
444 {
445  return (n ? n->timeout : -1);
446 }
447 
455 gint
457 {
458  return (n ? n->category : -1);
459 }
460 
470 int
471 nvti_set_oid (nvti_t *n, const gchar *oid)
472 {
473  if (!n)
474  return (-1);
475 
476  if (n->oid)
477  g_free (n->oid);
478  n->oid = g_strdup (oid);
479  return (0);
480 }
481 
491 int
492 nvti_set_name (nvti_t *n, const gchar *name)
493 {
494  if (!n)
495  return (-1);
496 
497  if (n->name)
498  g_free (n->name);
499  n->name = g_strdup (name);
500  return (0);
501 }
502 
512 int
513 nvti_set_cve (nvti_t *n, const gchar *cve)
514 {
515  if (!n)
516  return (-1);
517 
518  if (n->cve)
519  g_free (n->cve);
520  n->cve = g_strdup (cve);
521  return (0);
522 }
523 
533 int
534 nvti_set_bid (nvti_t *n, const gchar *bid)
535 {
536  if (!n)
537  return (-1);
538 
539  if (n->bid)
540  g_free (n->bid);
541  n->bid = g_strdup (bid);
542  return (0);
543 }
544 
554 int
555 nvti_set_xref (nvti_t *n, const gchar *xref)
556 {
557  if (!n)
558  return (-1);
559 
560  if (n->xref)
561  g_free (n->xref);
562  if (xref && xref[0])
563  n->xref = g_strdup (xref);
564  else
565  n->xref = NULL;
566  return (0);
567 }
568 
578 int
579 nvti_set_tag (nvti_t *n, const gchar *tag)
580 {
581  if (!n)
582  return (-1);
583 
584  if (n->tag)
585  g_free (n->tag);
586  if (tag && tag[0])
587  n->tag = g_strdup (tag);
588  else
589  n->tag = NULL;
590  return (0);
591 }
592 
602 int
603 nvti_set_cvss_base (nvti_t *n, const gchar *cvss_base)
604 {
605  if (!n)
606  return (-1);
607 
608  if (n->cvss_base)
609  g_free (n->cvss_base);
610  if (cvss_base && cvss_base[0])
611  n->cvss_base = g_strdup (cvss_base);
612  else
613  n->cvss_base = NULL;
614  return (0);
615 }
616 
627 int
628 nvti_set_dependencies (nvti_t *n, const gchar *dependencies)
629 {
630  if (!n)
631  return (-1);
632 
633  if (n->dependencies)
634  g_free (n->dependencies);
635  if (dependencies && dependencies[0])
636  n->dependencies = g_strdup (dependencies);
637  else
638  n->dependencies = NULL;
639  return (0);
640 }
641 
652 int
653 nvti_set_required_keys (nvti_t *n, const gchar *required_keys)
654 {
655  if (!n)
656  return (-1);
657 
658  if (n->required_keys)
659  g_free (n->required_keys);
660  if (required_keys && required_keys[0])
661  n->required_keys = g_strdup (required_keys);
662  else
663  n->required_keys = NULL;
664  return (0);
665 }
666 
677 int
678 nvti_set_mandatory_keys (nvti_t *n, const gchar *mandatory_keys)
679 {
680  if (!n)
681  return (-1);
682 
683  if (n->mandatory_keys)
684  g_free (n->mandatory_keys);
685  if (mandatory_keys && mandatory_keys[0])
686  n->mandatory_keys = g_strdup (mandatory_keys);
687  else
688  n->mandatory_keys = NULL;
689  return (0);
690 }
691 
702 int
703 nvti_set_excluded_keys (nvti_t *n, const gchar *excluded_keys)
704 {
705  if (!n)
706  return (-1);
707 
708  if (n->excluded_keys)
709  g_free (n->excluded_keys);
710  if (excluded_keys && excluded_keys[0])
711  n->excluded_keys = g_strdup (excluded_keys);
712  else
713  n->excluded_keys = NULL;
714  return (0);
715 }
716 
727 int
728 nvti_set_required_ports (nvti_t *n, const gchar *required_ports)
729 {
730  if (!n)
731  return (-1);
732 
733  if (n->required_ports)
734  g_free (n->required_ports);
735  if (required_ports && required_ports[0])
736  n->required_ports = g_strdup (required_ports);
737  else
738  n->required_ports = NULL;
739  return (0);
740 }
741 
752 int
753 nvti_set_required_udp_ports (nvti_t *n, const gchar *required_udp_ports)
754 {
755  if (!n)
756  return (-1);
757 
758  if (n->required_udp_ports)
759  g_free (n->required_udp_ports);
760  if (required_udp_ports && required_udp_ports[0])
761  n->required_udp_ports = g_strdup (required_udp_ports);
762  else
763  n->required_udp_ports = NULL;
764  return (0);
765 }
766 
776 int
777 nvti_set_family (nvti_t *n, const gchar *family)
778 {
779  if (!n)
780  return (-1);
781 
782  if (n->family)
783  g_free (n->family);
784  n->family = g_strdup (family);
785  return (0);
786 }
787 
797 int
798 nvti_set_timeout (nvti_t *n, const gint timeout)
799 {
800  if (!n)
801  return (-1);
802 
803  n->timeout = timeout;
804  return (0);
805 }
806 
816 int
817 nvti_set_category (nvti_t *n, const gint category)
818 {
819  if (!n)
820  return (-1);
821 
822  n->category = category;
823  return (0);
824 }
825 
835 int
836 nvti_add_cve (nvti_t *n, const gchar *cve_id)
837 {
838  gchar *old;
839 
840  if (!n)
841  return (1);
842  if (!cve_id)
843  return (2);
844 
845  old = n->cve;
846 
847  if (old)
848  {
849  n->cve = g_strdup_printf ("%s, %s", old, cve_id);
850  g_free (old);
851  }
852  else
853  n->cve = g_strdup (cve_id);
854 
855  return (0);
856 }
857 
867 int
868 nvti_add_bid (nvti_t *n, const gchar *bid_id)
869 {
870  gchar *old;
871 
872  if (!n)
873  return (1);
874  if (!bid_id)
875  return (2);
876 
877  old = n->bid;
878 
879  if (old)
880  {
881  n->bid = g_strdup_printf ("%s, %s", old, bid_id);
882  g_free (old);
883  }
884  else
885  n->bid = g_strdup (bid_id);
886 
887  return (0);
888 }
889 
899 int
900 nvti_add_required_keys (nvti_t *n, const gchar *key)
901 {
902  gchar *old;
903 
904  if (!n)
905  return (1);
906  if (!key)
907  return (2);
908 
909  old = n->required_keys;
910 
911  if (old)
912  {
913  n->required_keys = g_strdup_printf ("%s, %s", old, key);
914  g_free (old);
915  }
916  else
917  n->required_keys = g_strdup (key);
918 
919  return (0);
920 }
921 
931 int
932 nvti_add_mandatory_keys (nvti_t *n, const gchar *key)
933 {
934  gchar *old;
935 
936  if (!n)
937  return (1);
938  if (!key)
939  return (2);
940 
941  old = n->mandatory_keys;
942 
943  if (old)
944  {
945  n->mandatory_keys = g_strdup_printf ("%s, %s", old, key);
946  g_free (old);
947  }
948  else
949  n->mandatory_keys = g_strdup (key);
950 
951  return (0);
952 }
953 
963 int
964 nvti_add_excluded_keys (nvti_t *n, const gchar *key)
965 {
966  gchar *old;
967 
968  if (!n)
969  return (1);
970  if (!key)
971  return (2);
972 
973  old = n->excluded_keys;
974 
975  if (old)
976  {
977  n->excluded_keys = g_strdup_printf ("%s, %s", old, key);
978  g_free (old);
979  }
980  else
981  n->excluded_keys = g_strdup (key);
982 
983  return (0);
984 }
985 
995 int
996 nvti_add_required_ports (nvti_t *n, const gchar *port)
997 {
998  gchar *old;
999 
1000  if (!n)
1001  return (1);
1002  if (!port)
1003  return (2);
1004 
1005  old = n->required_ports;
1006 
1007  if (old)
1008  {
1009  n->required_ports = g_strdup_printf ("%s, %s", old, port);
1010  g_free (old);
1011  }
1012  else
1013  n->required_ports = g_strdup (port);
1014 
1015  return (0);
1016 }
1017 
1027 int
1028 nvti_add_required_udp_ports (nvti_t *n, const gchar *port)
1029 {
1030  gchar *old;
1031 
1032  if (!n)
1033  return (1);
1034  if (!port)
1035  return (2);
1036 
1037  old = n->required_udp_ports;
1038 
1039  if (old)
1040  {
1041  n->required_udp_ports = g_strdup_printf ("%s, %s", old, port);
1042  g_free (old);
1043  }
1044  else
1045  n->required_udp_ports = g_strdup (port);
1046 
1047  return (0);
1048 }
1049 
1059 int
1061 {
1062  if (!n)
1063  return (-1);
1064 
1065  n->prefs = g_slist_append (n->prefs, np);
1066  return (0);
1067 }
1068 
1069 /* Collections of nvtis. */
1070 
1076 static void
1078 {
1079  nvti_free ((nvti_t *) nvti);
1080 }
1081 
1087 nvtis_t *
1089 {
1090  return g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1092 }
1093 
1099 void
1101 {
1102  if (nvtis)
1103  g_hash_table_destroy (nvtis);
1104 }
1105 
1112 void
1114 {
1115  if (nvti)
1116  g_hash_table_insert (nvtis, (gpointer) nvti_oid (nvti), (gpointer) nvti);
1117 }
1118 
1127 nvti_t *
1128 nvtis_lookup (nvtis_t *nvtis, const char *oid)
1129 {
1130  return g_hash_table_lookup (nvtis, oid);
1131 }
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition: nvti.c:817
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition: nvti.c:142
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition: nvti.c:114
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
Definition: nvti.c:275
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition: nvti.c:167
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition: nvti.c:373
gchar * required_keys
List of required KB keys of this NVT.
Definition: nvti.h:76
int nvti_set_cvss_base(nvti_t *n, const gchar *cvss_base)
Set the CVSS base of an NVT.
Definition: nvti.c:603
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:156
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition: nvti.c:777
gchar * nvtpref_type(const nvtpref_t *np)
Get the Type of a NVT Preference.
Definition: nvti.c:128
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition: nvti.c:728
nvtpref_t * nvtpref_new(gchar *name, gchar *type, gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition: nvti.c:68
gchar * nvti_cve(const nvti_t *n)
Get the CVE references.
Definition: nvti.c:247
gchar * cvss_base
CVSS base score for this NVT.
Definition: nvti.h:73
gchar * name
Name of the preference.
Definition: nvti.h:41
The structure of a information record that corresponds to a NVT.
Definition: nvti.h:62
gint timeout
Default timeout time for this NVT.
Definition: nvti.h:86
const nvtpref_t * nvti_pref(const nvti_t *n, guint p)
Get the n'th preferences of the NVT.
Definition: nvti.c:429
gchar * bid
List of Bugtraq IDs, this NVT corresponds to.
Definition: nvti.h:68
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition: nvti.c:456
gchar * name
The name.
Definition: nvti.h:65
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition: nvti.c:91
int nvti_set_timeout(nvti_t *n, const gint timeout)
Set the timeout of a NVT Info.
Definition: nvti.c:798
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:579
int nvti_add_required_udp_ports(nvti_t *n, const gchar *port)
Add a required udp port of a NVT.
Definition: nvti.c:1028
gchar * oid
Object ID.
Definition: nvti.h:64
gint nvti_timeout(const nvti_t *n)
Get the timeout for this NVT.
Definition: nvti.c:443
int nvti_add_cve(nvti_t *n, const gchar *cve_id)
Add a single CVE ID of a NVT.
Definition: nvti.c:836
int nvti_set_cve(nvti_t *n, const gchar *cve)
Set the CVE references of a NVT.
Definition: nvti.c:513
gint category
The category, this NVT belongs to.
Definition: nvti.h:87
static void free_nvti_for_hash_table(gpointer nvti)
Free an NVT Info, for g_hash_table_destroy.
Definition: nvti.c:1077
gchar * nvti_tag(const nvti_t *n)
Get the tag.
Definition: nvti.c:289
gchar * nvti_family(const nvti_t *n)
Get the family name.
Definition: nvti.c:401
gchar * type
Preference type.
Definition: nvti.h:40
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition: nvti.c:317
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition: nvti.c:359
gchar * required_udp_ports
List of required UDP ports of this NVT.
Definition: nvti.h:81
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition: nvti.c:345
void nvtis_add(nvtis_t *nvtis, nvti_t *nvti)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:1113
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:471
gchar * cve
List of CVEs, this NVT corresponds to.
Definition: nvti.h:67
int nvti_add_mandatory_keys(nvti_t *n, const gchar *key)
Add a mandatory key of a NVT.
Definition: nvti.c:932
int nvti_add_bid(nvti_t *n, const gchar *bid_id)
Add a single BID ID of a NVT.
Definition: nvti.c:868
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition: nvti.c:678
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition: nvti.c:492
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
Definition: nvti.c:555
Protos and data structures for NVT Information data sets.
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition: nvti.c:1060
gchar * excluded_keys
List of excluded KB keys of this NVT.
Definition: nvti.h:78
int nvti_set_bid(nvti_t *n, const gchar *bid)
Set the bid references of a NVT.
Definition: nvti.c:534
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition: nvti.c:387
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:753
gchar * dependencies
List of dependencies of this NVT.
Definition: nvti.h:75
gchar * dflt
Default value of the preference.
Definition: nvti.h:42
int nvti_add_excluded_keys(nvti_t *n, const gchar *key)
Add a excluded key of a NVT.
Definition: nvti.c:964
nvtis_t * nvtis_new(void)
Make a collection of NVT Infos.
Definition: nvti.c:1088
gchar * required_ports
List of required ports of this NVT.
Definition: nvti.h:79
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition: nvti.c:703
void nvtis_free(nvtis_t *nvtis)
Free a collection of NVT Infos.
Definition: nvti.c:1100
gchar * family
Family the NVT belongs to.
Definition: nvti.h:88
The structure for a preference of a NVT.
Definition: nvti.h:38
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition: nvti.c:628
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition: nvti.c:331
gchar * mandatory_keys
List of mandatory KB keys of this NVT.
Definition: nvti.h:77
gchar * tag
List of tags attached to this NVT.
Definition: nvti.h:72
int nvti_add_required_ports(nvti_t *n, const gchar *port)
Add a required port of a NVT.
Definition: nvti.c:996
nvti_t * nvtis_lookup(nvtis_t *nvtis, const char *oid)
Add an NVT Info to a collection of NVT Infos.
Definition: nvti.c:1128
int nvti_add_required_keys(nvti_t *n, const gchar *key)
Add a required key of a NVT.
Definition: nvti.c:900
guint nvti_pref_len(const nvti_t *n)
Get the number of preferences of the NVT.
Definition: nvti.c:414
gchar * nvti_cvss_base(const nvti_t *n)
Get the CVSS base.
Definition: nvti.c:303
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:219
gchar * nvti_bid(const nvti_t *n)
Get the bid references.
Definition: nvti.c:261
GHashTable nvtis_t
A collection of information records corresponding to NVTs.
Definition: nvti.h:188
gchar * xref
List of Cross-references, this NVT corresponds to.
Definition: nvti.h:70
port_protocol_t type
Definition: networking.h:55
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition: nvti.c:653
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition: nvti.c:233
GSList * prefs
Collection of NVT preferences.
Definition: nvti.h:83