"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "base/nvti.c" between
gvm-libs-10.0.1.tar.gz and gvm-libs-11.0.0.tar.gz

About: GVM Libraries for the Greenbone Vulnerability Management (GVM-10) framework (derived from the former openvas-libraries).

nvti.c  (gvm-libs-10.0.1):nvti.c  (gvm-libs-11.0.0)
skipping to change at line 47 skipping to change at line 47
* @file * @file
* @brief Implementation of API to handle NVT Info datasets * @brief Implementation of API to handle NVT Info datasets
* *
* This file contains all methods to handle NVT Information datasets * This file contains all methods to handle NVT Information datasets
* (nvti_t). * (nvti_t).
* *
* The module consequently uses glib datatypes and api for memory * The module consequently uses glib datatypes and api for memory
* management etc. * management etc.
*/ */
/* For strptime in time.h. */
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#include "nvti.h" #include "nvti.h"
#include <stdio.h> #include <stdio.h> // for sscanf
#include <string.h> // for strcmp
#include <strings.h> // for strcasecmp
#include <time.h> // for strptime
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "lib nvti" #define G_LOG_DOMAIN "lib nvti"
/* VT references */
/**
* @brief The structure for a cross reference of a VT.
*
* The elements of this structure should only be accessed by the
* respective functions.
*/
typedef struct vtref
{
gchar *type; ///< Reference type ("cve", "bid", ...)
gchar *ref_id; ///< Actual reference ID ("CVE-2018-1234", etc)
gchar *ref_text; ///< Optional additional text
} vtref_t;
/**
* @brief Create a new vtref structure filled with the given values.
*
* @param type The type to be set.
*
* @param ref_id The actual reference to be set.
*
* @param ref_text The optional text accompanying a reference.
*
* @return NULL in case the memory could not be allocated.
* Else a vtref structure which needs to be
* released using @ref vtref_free .
*/
vtref_t *
vtref_new (const gchar *type, const gchar *ref_id, const gchar *ref_text)
{
vtref_t *ref = g_malloc0 (sizeof (vtref_t));
if (type)
ref->type = g_strdup (type);
if (ref_id)
ref->ref_id = g_strdup (ref_id);
if (ref_text)
ref->ref_text = g_strdup (ref_text);
return (ref);
}
/**
* @brief Free memory of a vtref structure.
*
* @param ref The structure to be freed.
*/
void
vtref_free (vtref_t *ref)
{
if (!ref)
return;
g_free (ref->type);
g_free (ref->ref_id);
g_free (ref->ref_text);
g_free (ref);
}
/**
* @brief Get the type of a reference.
*
* @param r The VT Reference structure of which the type should
* be returned.
*
* @return The type string. Don't free this.
*/
const gchar *
vtref_type (const vtref_t *r)
{
return (r ? r->type : NULL);
}
/**
* @brief Get the id of a reference.
*
* @param r The VT Reference structure of which the id should
* be returned.
*
* @return The id string. Don't free this.
*/
const gchar *
vtref_id (const vtref_t *r)
{
return (r ? r->ref_id : NULL);
}
/**
* @brief Get the text of a reference.
*
* @param r The VT Reference structure of which the id should
* be returned.
*
* @return The id string. Don't free this.
*/
const gchar *
vtref_text (const vtref_t *r)
{
return (r ? r->ref_text : NULL);
}
/* Support function for timestamps */
/**
* @brief Try convert an NVT tag time string into epoch time
* or return 0 upon parse errors.
*
* @param[in] str_time Time stamp as string in one of the forms used in NVTs.
*
* @return Time as seconds since the epoch.
*/
static time_t
parse_nvt_timestamp (const gchar *str_time)
{
time_t epoch_time;
int offset;
struct tm tm;
if ((strcmp ((char *) str_time, "") == 0)
|| (strcmp ((char *) str_time, "$Date: $") == 0)
|| (strcmp ((char *) str_time, "$Date$") == 0)
|| (strcmp ((char *) str_time, "$Date:$") == 0)
|| (strcmp ((char *) str_time, "$Date") == 0)
|| (strcmp ((char *) str_time, "$$") == 0))
{
return 0;
}
/* Parse the time. */
/* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
/* $Date: 2012-02-17 16:05:26 +0100 (Fr, 17. Feb 2012) $ */
/* $Date: Fri, 11 Nov 2011 14:42:28 +0100 $ */
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "%F %T %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %F %T %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "%a %b %d %T %Y %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %a, %d %b %Y %T %z", &tm)
== NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %a %b %d %T %Y %z",
&tm)
== NULL)
{
g_warning ("%s: Failed to parse time: %s", __FUNCTION__,
str_time);
return 0;
}
}
}
}
}
epoch_time = mktime (&tm);
if (epoch_time == -1)
{
g_warning ("%s: Failed to make time: %s", __FUNCTION__, str_time);
return 0;
}
/* Get the timezone offset from the str_time. */
if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
!= 1)
&& (sscanf ((char *) str_time, "$Date: %*u-%*u-%*u %*u:%*u:%*u %d%*[^]]",
&offset)
!= 1)
&& (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
&offset)
!= 1)
&& (sscanf ((char *) str_time,
"$Date: %*s %*s %*s %*u %*u:%*u:%*u %d%*[^]]", &offset)
!= 1)
&& (sscanf ((char *) str_time,
"$Date: %*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]", &offset)
!= 1))
{
g_warning ("%s: Failed to parse timezone offset: %s", __FUNCTION__,
str_time);
return 0;
}
/* Use the offset to convert to UTC. */
if (offset < 0)
{
epoch_time += ((-offset) / 100) * 60 * 60;
epoch_time += ((-offset) % 100) * 60;
}
else if (offset > 0)
{
epoch_time -= (offset / 100) * 60 * 60;
epoch_time -= (offset % 100) * 60;
}
return epoch_time;
}
/* VT Information */
/**
* @brief The structure of a information record that corresponds to a NVT.
*/
typedef struct nvti
{
gchar *oid; /**< @brief Object ID */
gchar *name; /**< @brief The name */
gchar *summary; /**< @brief The summary */
gchar *insight; /**< @brief The insight */
gchar *affected; /**< @brief Affected systems */
gchar *impact; /**< @brief Impact of vulnerability */
time_t creation_time; /**< @brief Time of creation, seconds since epoch */
time_t modification_time; /**< @brief Time of last change, sec. since epoch */
gchar *solution; /**< @brief The solution */
gchar *solution_type; /**< @brief The solution type */
gchar *tag; /**< @brief List of tags attached to this NVT */
gchar *cvss_base; /**< @brief CVSS base score for this NVT. */
gchar *dependencies; /**< @brief List of dependencies of this NVT */
gchar *required_keys; /**< @brief List of required KB keys of this NVT */
gchar *mandatory_keys; /**< @brief List of mandatory KB keys of this NVT */
gchar *excluded_keys; /**< @brief List of excluded KB keys of this NVT */
gchar *required_ports; /**< @brief List of required ports of this NVT */
gchar
*required_udp_ports; /**< @brief List of required UDP ports of this NVT*/
gchar *detection; /**< @brief Detection description */
gchar *qod_type; /**< @brief Quality of detection type */
GSList *refs; /**< @brief Collection of VT references */
GSList *prefs; /**< @brief Collection of NVT preferences */
// The following are not settled yet.
gint timeout; /**< @brief Default timeout time for this NVT */
gint category; /**< @brief The category, this NVT belongs to */
gchar *family; /**< @brief Family the NVT belongs to */
} nvti_t;
/**
* @brief Add a reference to the VT Info.
*
* @param vt The VT Info structure.
*
* @param ref The VT reference to add.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_add_vtref (nvti_t *vt, vtref_t *ref)
{
if (!vt)
return (-1);
vt->refs = g_slist_append (vt->refs, ref);
return (0);
}
/* VT preferences */
/**
* @brief The structure for a preference of a NVT.
*/
typedef struct nvtpref
{
int id; ///< Preference ID
gchar *type; ///< Preference type
gchar *name; ///< Name of the preference
gchar *dflt; ///< Default value of the preference
} nvtpref_t;
/** /**
* @brief Create a new nvtpref structure filled with the given values. * @brief Create a new nvtpref structure filled with the given values.
* *
* @param id The ID to be set.
*
* @param name The name to be set. A copy will created of this. * @param name The name to be set. A copy will created of this.
* *
* @param type The type to be set. A copy will created of this. * @param type The type to be set. A copy will created of this.
* *
* @param dflt The default to be set. A copy will created of this. * @param dflt The default to be set. A copy will created of this.
* *
* @return NULL in case the memory could not be allocated. * @return NULL in case the memory could not be allocated.
* Else a nvtpref structure which needs to be * Else a nvtpref structure which needs to be
* released using @ref nvtpref_free . * released using @ref nvtpref_free .
*/ */
nvtpref_t * nvtpref_t *
nvtpref_new (gchar *name, gchar *type, gchar *dflt) nvtpref_new (int id, gchar *name, gchar *type, gchar *dflt)
{ {
nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t)); nvtpref_t *np = g_malloc0 (sizeof (nvtpref_t));
if (!np) np->id = id;
return NULL;
if (name) if (name)
np->name = g_strdup (name); np->name = g_strdup (name);
if (type) if (type)
np->type = g_strdup (type); np->type = g_strdup (type);
if (dflt) if (dflt)
np->dflt = g_strdup (dflt); np->dflt = g_strdup (dflt);
return (np); return (np);
} }
skipping to change at line 96 skipping to change at line 383
* @brief Free memory of a nvtpref structure. * @brief Free memory of a nvtpref structure.
* *
* @param np The structure to be freed. * @param np The structure to be freed.
*/ */
void void
nvtpref_free (nvtpref_t *np) nvtpref_free (nvtpref_t *np)
{ {
if (!np) if (!np)
return; return;
if (np->name) g_free (np->name);
g_free (np->name); g_free (np->type);
if (np->type) g_free (np->dflt);
g_free (np->type);
if (np->dflt)
g_free (np->dflt);
g_free (np); g_free (np);
} }
/** /**
* @brief Get the ID of a NVT Preference.
*
* @param np The NVT Pref structure of which the Name should
* be returned.
*
* @return The ID value.
*/
int
nvtpref_id (const nvtpref_t *np)
{
return np ? np->id : -1;
}
/**
* @brief Get the Name of a NVT Preference. * @brief Get the Name of a NVT Preference.
* *
* @param np The NVT Pref structure of which the Name should * @param np The NVT Pref structure of which the Name should
* be returned. * be returned.
* *
* @return The name string. Don't free this. * @return The name string. Don't free this.
*/ */
gchar * gchar *
nvtpref_name (const nvtpref_t *np) nvtpref_name (const nvtpref_t *np)
{ {
skipping to change at line 172 skipping to change at line 470
* @brief Free memory of a nvti structure. * @brief Free memory of a nvti structure.
* *
* @param n The structure to be freed. * @param n The structure to be freed.
*/ */
void void
nvti_free (nvti_t *n) nvti_free (nvti_t *n)
{ {
if (!n) if (!n)
return; return;
if (n->oid) g_free (n->oid);
g_free (n->oid); g_free (n->name);
if (n->name) g_free (n->summary);
g_free (n->name); g_free (n->insight);
if (n->cve) g_free (n->affected);
g_free (n->cve); g_free (n->impact);
if (n->bid) g_free (n->solution);
g_free (n->bid); g_free (n->solution_type);
if (n->xref) g_free (n->tag);
g_free (n->xref); g_free (n->cvss_base);
if (n->tag) g_free (n->dependencies);
g_free (n->tag); g_free (n->required_keys);
if (n->cvss_base) g_free (n->mandatory_keys);
g_free (n->cvss_base); g_free (n->excluded_keys);
if (n->dependencies) g_free (n->required_ports);
g_free (n->dependencies); g_free (n->required_udp_ports);
if (n->required_keys) g_free (n->detection);
g_free (n->required_keys); g_free (n->qod_type);
if (n->mandatory_keys) g_free (n->family);
g_free (n->mandatory_keys); g_slist_free_full (n->refs, (void (*) (void *)) vtref_free);
if (n->excluded_keys) g_slist_free_full (n->prefs, (void (*) (void *)) nvtpref_free);
g_free (n->excluded_keys);
if (n->required_ports)
g_free (n->required_ports);
if (n->required_udp_ports)
g_free (n->required_udp_ports);
if (n->family)
g_free (n->family);
if (n->prefs)
{
int i, len = g_slist_length (n->prefs);
for (i = 0; i < len; i++)
nvtpref_free (g_slist_nth_data (n->prefs, i));
g_slist_free (n->prefs);
}
g_free (n); g_free (n);
} }
/** /**
* @brief Get the OID string. * @brief Get the OID string.
* *
* @param n The NVT Info structure of which the OID should * @param n The NVT Info structure of which the OID should
* be returned. * be returned.
* *
* @return The OID string. Don't free this. * @return The OID string. Don't free this.
skipping to change at line 239 skipping to change at line 523
* *
* @return The name string. Don't free this. * @return The name string. Don't free this.
*/ */
gchar * gchar *
nvti_name (const nvti_t *n) nvti_name (const nvti_t *n)
{ {
return (n ? n->name : NULL); return (n ? n->name : NULL);
} }
/** /**
* @brief Get the CVE references. * @brief Get the summary.
* *
* @param n The NVT Info structure of which the name should * @param n The NVT Info structure of which the summary should
* be returned. * be returned.
* *
* @return The CVE list as string. Don't free this. * @return The summary string. Don't free this.
*/ */
gchar * gchar *
nvti_cve (const nvti_t *n) nvti_summary (const nvti_t *n)
{ {
return (n ? n->cve : NULL); return (n ? n->summary : NULL);
} }
/** /**
* @brief Get the bid references. * @brief Get the text about insight.
* *
* @param n The NVT Info structure of which the name should * @param n The NVT Info structure of which the insight description should
* be returned. * be returned.
* *
* @return The bid list as string. Don't free this. * @return The insight string. Don't free this.
*/ */
gchar * gchar *
nvti_bid (const nvti_t *n) nvti_insight (const nvti_t *n)
{ {
return (n ? n->bid : NULL); return (n ? n->insight : NULL);
} }
/** /**
* @brief Get the xref's. * @brief Get the text about affected systems.
* *
* @param n The NVT Info structure of which the name should * @param n The NVT Info structure of which the affected description should
* be returned. * be returned.
* *
* @return The xref string. Don't free this. * @return The affected string. Don't free this.
*/ */
gchar * gchar *
nvti_xref (const nvti_t *n) nvti_affected (const nvti_t *n)
{ {
return (n ? n->xref : NULL); return (n ? n->affected : NULL);
} }
/** /**
* @brief Get the tag. * @brief Get the text about impact.
* *
* @param n The NVT Info structure of which the name should * @param n The NVT Info structure of which the impact description should
* be returned.
*
* @return The impact string. Don't free this.
*/
gchar *
nvti_impact (const nvti_t *n)
{
return (n ? n->impact : NULL);
}
/**
* @brief Get the creation time.
*
* @param n The NVT Info structure of which the creation time should
* be returned.
*
* @return The creation time in seconds since epoch.
*/
time_t
nvti_creation_time (const nvti_t *n)
{
return (n ? n->creation_time : 0);
}
/**
* @brief Get the modification time.
*
* @param n The NVT Info structure of which the modification time should
* be returned.
*
* @return The modification time in seconds since epoch.
*/
time_t
nvti_modification_time (const nvti_t *n)
{
return (n ? n->modification_time : 0);
}
/**
* @brief Get the number of references of the NVT.
*
* @param n The NVT Info structure.
*
* @return The number of references.
*/
guint
nvti_vtref_len (const nvti_t *n)
{
return (n ? g_slist_length (n->refs) : 0);
}
/**
* @brief Get the n'th reference of the NVT.
*
* @param n The NVT Info structure.
*
* @param p The position of the reference to return.
*
* @return The reference. NULL on error.
*/
vtref_t *
nvti_vtref (const nvti_t *n, guint p)
{
return (n ? g_slist_nth_data (n->refs, p) : NULL);
}
/**
* @brief Get references as string.
*
* @param n The NVT Info structure of which the references should
* be returned.
*
* @param type Optional type to collect. If NULL, all types are collected.
*
* @param exclude_types Optional CSC list of types to exclude from collection.
* If NULL, no types are excluded.
*
* @param use_types If 0, then a simple comma separated list will be returned.
* If not 0, then for each reference the syntax "type:id" is
* applied.
*
* @return The references as string. This needs to be free'd.
* The format of the string depends on the "use_types" parameter.
* If use_types is 0 it is a comma-separated list "id, id, id"
* is returned.
* If use_types is not 0 a comma-separated list like
* "type:id, type:id, type:id" is returned.
* NULL is returned in case n is NULL.
*/
gchar *
nvti_refs (const nvti_t *n, const gchar *type, const gchar *exclude_types,
guint use_types)
{
gchar *refs, *refs2, **exclude_item;
vtref_t *ref;
guint i, exclude;
gchar **exclude_split;
if (!n)
return (NULL);
refs = NULL;
refs2 = NULL;
exclude = 0;
if (exclude_types && exclude_types[0])
exclude_split = g_strsplit (exclude_types, ",", 0);
else
exclude_split = NULL;
for (i = 0; i < g_slist_length (n->refs); i++)
{
ref = g_slist_nth_data (n->refs, i);
if (type && strcasecmp (ref->type, type) != 0)
continue;
if (exclude_split)
{
exclude = 0;
for (exclude_item = exclude_split; *exclude_item; exclude_item++)
{
if (strcasecmp (g_strstrip (*exclude_item), ref->type) == 0)
{
exclude = 1;
break;
}
}
}
if (!exclude)
{
if (use_types)
{
if (refs)
refs2 =
g_strdup_printf ("%s, %s:%s", refs, ref->type, ref->ref_id);
else
refs2 = g_strdup_printf ("%s:%s", ref->type, ref->ref_id);
}
else
{
if (refs)
refs2 = g_strdup_printf ("%s, %s", refs, ref->ref_id);
else
refs2 = g_strdup_printf ("%s", ref->ref_id);
}
g_free (refs);
refs = refs2;
}
}
g_strfreev (exclude_split);
return (refs);
}
/**
* @brief Get the solution.
*
* @param n The NVT Info structure of which the solution should
* be returned.
*
* @return The solution string. Don't free this.
*/
gchar *
nvti_solution (const nvti_t *n)
{
return (n ? n->solution : NULL);
}
/**
* @brief Get the solution type.
*
* @param n The NVT Info structure of which the solution type should
* be returned.
*
* @return The solution type string. Don't free this.
*/
gchar *
nvti_solution_type (const nvti_t *n)
{
return (n ? n->solution_type : NULL);
}
/**
* @brief Get the tags.
*
* @param n The NVT Info structure of which the tags should
* be returned. * be returned.
* *
* @return The tags string. Don't free this. * @return The tags string. Don't free this.
*/ */
gchar * gchar *
nvti_tag (const nvti_t *n) nvti_tag (const nvti_t *n)
{ {
return (n ? n->tag : NULL); return (n ? n->tag : NULL);
} }
skipping to change at line 393 skipping to change at line 865
* *
* @return The required udp ports string. Don't free this. * @return The required udp ports string. Don't free this.
*/ */
gchar * gchar *
nvti_required_udp_ports (const nvti_t *n) nvti_required_udp_ports (const nvti_t *n)
{ {
return (n ? n->required_udp_ports : NULL); return (n ? n->required_udp_ports : NULL);
} }
/** /**
* @brief Get the text about detection.
*
* @param n The NVT Info structure of which the detection should
* be returned.
*
* @return The detection string. Don't free this.
*/
gchar *
nvti_detection (const nvti_t *n)
{
return (n ? n->detection : NULL);
}
/**
* @brief Get the QoD type.
*
* @param n The NVT Info structure of which the QoD type should
* be returned.
*
* @return The QoD type as string. Don't free this.
*/
gchar *
nvti_qod_type (const nvti_t *n)
{
return (n ? n->qod_type : NULL);
}
/**
* @brief Get the family name. * @brief Get the family name.
* *
* @param n The NVT Info structure of which the name should * @param n The NVT Info structure of which the name should
* be returned. * be returned.
* *
* @return The family name string. Don't free this. * @return The family name string. Don't free this.
*/ */
gchar * gchar *
nvti_family (const nvti_t *n) nvti_family (const nvti_t *n)
{ {
skipping to change at line 426 skipping to change at line 926
return (n ? g_slist_length (n->prefs) : 0); return (n ? g_slist_length (n->prefs) : 0);
} }
/** /**
* @brief Get the n'th preferences of the NVT. * @brief Get the n'th preferences of the NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param p The position of the preference to return. * @param p The position of the preference to return.
* *
* @return The number of preferences. NULL if * @return The preference. NULL on error.
*/ */
const nvtpref_t * const nvtpref_t *
nvti_pref (const nvti_t *n, guint p) nvti_pref (const nvti_t *n, guint p)
{ {
return (n ? g_slist_nth_data (n->prefs, p) : NULL); return (n ? g_slist_nth_data (n->prefs, p) : NULL);
} }
/** /**
* @brief Get the timeout for this NVT. * @brief Get the timeout for this NVT.
* *
skipping to change at line 504 skipping to change at line 1004
if (!n) if (!n)
return (-1); return (-1);
if (n->name) if (n->name)
g_free (n->name); g_free (n->name);
n->name = g_strdup (name); n->name = g_strdup (name);
return (0); return (0);
} }
/** /**
* @brief Set the CVE references of a NVT. * @brief Set the summary of a NVT.
*
* @param n The NVT Info structure.
*
* @param solution The summary to set. A copy will be created from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_summary (nvti_t *n, const gchar *summary)
{
if (!n)
return (-1);
if (n->summary)
g_free (n->summary);
n->summary = g_strdup (summary);
return (0);
}
/**
* @brief Set the insight text of a NVT.
*
* @param n The NVT Info structure.
*
* @param insight The insight text to set. A copy will be created from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_insight (nvti_t *n, const gchar *insight)
{
if (!n)
return (-1);
if (n->insight)
g_free (n->insight);
n->insight = g_strdup (insight);
return (0);
}
/**
* @brief Set the affected text of a NVT.
*
* @param n The NVT Info structure.
*
* @param affected The affected text to set. A copy will be created from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_affected (nvti_t *n, const gchar *affected)
{
if (!n)
return (-1);
if (n->affected)
g_free (n->affected);
n->affected = g_strdup (affected);
return (0);
}
/**
* @brief Set the impact text of a NVT.
*
* @param n The NVT Info structure.
*
* @param affected The impact text to set. A copy will be created from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_impact (nvti_t *n, const gchar *impact)
{
if (!n)
return (-1);
if (n->impact)
g_free (n->impact);
n->impact = g_strdup (impact);
return (0);
}
/**
* @brief Set the creation time of a NVT.
*
* @param n The NVT Info structure.
*
* @param creation_time The creation time to set.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_creation_time (nvti_t *n, const time_t creation_time)
{
if (!n)
return (-1);
n->creation_time = creation_time;
return (0);
}
/**
* @brief Set the modification time of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param cve The cve list to set. A copy will be created from this. * @param modification_time The modification time to set.
* *
* @return 0 for success. Anything else indicates an error. * @return 0 for success. Anything else indicates an error.
*/ */
int int
nvti_set_cve (nvti_t *n, const gchar *cve) nvti_set_modification_time (nvti_t *n, const time_t modification_time)
{ {
if (!n) if (!n)
return (-1); return (-1);
if (n->cve) n->modification_time = modification_time;
g_free (n->cve);
n->cve = g_strdup (cve);
return (0); return (0);
} }
/** /**
* @brief Set the bid references of a NVT. * @brief Set the solution of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param bid The bid to set. A copy will be created from this. * @param solution The solution to set. A copy will be created from this.
* *
* @return 0 for success. Anything else indicates an error. * @return 0 for success. Anything else indicates an error.
*/ */
int int
nvti_set_bid (nvti_t *n, const gchar *bid) nvti_set_solution (nvti_t *n, const gchar *solution)
{ {
if (!n) if (!n)
return (-1); return (-1);
if (n->bid) if (n->solution)
g_free (n->bid); g_free (n->solution);
n->bid = g_strdup (bid); n->solution = g_strdup (solution);
return (0); return (0);
} }
/** /**
* @brief Set the xrefs of a NVT. * @brief Set the solution type of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param xref The xrefs to set. A copy will be created from this. * @param solution_type The solution type to set. A copy will be created
* from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_solution_type (nvti_t *n, const gchar *solution_type)
{
if (!n)
return (-1);
if (n->solution_type)
g_free (n->solution_type);
n->solution_type = g_strdup (solution_type);
return (0);
}
/**
* @brief Add a tag to the NVT tags.
* The tag names "last_modification" and "creation_date" are
* treated special: The value is expected to be a timestamp
* and it is being converted to seconds since epoch before
* added as a tag value.
* The tag name "cvss_base" will be ignored and not added.
*
* @param n The NVT Info structure.
*
* @param name The tag name. A copy will be created from this.
*
* @param value The tag value. A copy will be created from this.
* *
* @return 0 for success. Anything else indicates an error. * @return 0 for success. Anything else indicates an error.
*/ */
int int
nvti_set_xref (nvti_t *n, const gchar *xref) nvti_add_tag (nvti_t *n, const gchar *name, const gchar *value)
{ {
gchar *newvalue = NULL;
if (!n) if (!n)
return (-1); return (-1);
if (n->xref) if (!name || !name[0])
g_free (n->xref); return (-2);
if (xref && xref[0])
n->xref = g_strdup (xref); if (!value || !value[0])
return (-3);
if (!strcmp (name, "last_modification"))
{
nvti_set_modification_time (n, parse_nvt_timestamp (value));
newvalue = g_strdup_printf ("%i", (int) nvti_modification_time (n));
}
else if (!strcmp (name, "creation_date"))
{
nvti_set_creation_time (n, parse_nvt_timestamp (value));
newvalue = g_strdup_printf ("%i", (int) nvti_creation_time (n));
}
else if (!strcmp (name, "cvss_base"))
{
/* Ignore this tag because it is not being used.
* It is redundant with the tag cvss_base_vector from which
* it is computed.
* Once GOS 6 and GVM 11 are retired, all set_tag commands
* in the NASL scripts can be removed that set "cvss_base".
* Once this happened this exception can be removed from the code.
*/
return (0);
}
if (n->tag)
{
gchar *newtag;
newtag =
g_strconcat (n->tag, "|", name, "=", newvalue ? newvalue : value, NULL);
g_free (n->tag);
n->tag = newtag;
}
else else
n->xref = NULL; n->tag = g_strconcat (name, "=", newvalue ? newvalue : value, NULL);
if (newvalue)
g_free (newvalue);
return (0); return (0);
} }
/** /**
* @brief Set the tags of a NVT. * @brief Set the tags of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param tag The tags to set. A copy will be created from this. * @param tag The tags to set. A copy will be created from this.
* *
skipping to change at line 768 skipping to change at line 1437
if (n->required_udp_ports) if (n->required_udp_ports)
g_free (n->required_udp_ports); g_free (n->required_udp_ports);
if (required_udp_ports && required_udp_ports[0]) if (required_udp_ports && required_udp_ports[0])
n->required_udp_ports = g_strdup (required_udp_ports); n->required_udp_ports = g_strdup (required_udp_ports);
else else
n->required_udp_ports = NULL; n->required_udp_ports = NULL;
return (0); return (0);
} }
/** /**
* @brief Set the detection text of a NVT.
*
* @param n The NVT Info structure.
*
* @param detection The detection text to set. A copy will be created from this.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_detection (nvti_t *n, const gchar *detection)
{
if (!n)
return (-1);
if (n->detection)
g_free (n->detection);
n->detection = g_strdup (detection);
return (0);
}
/**
* @brief Set the QoD type of a NVT.
*
* @param n The NVT Info structure.
*
* @param qod_type The QoD type to set. A copy will be created from this.
* The string is not checked, any string is accepted as type.
*
* @return 0 for success. Anything else indicates an error.
*/
int
nvti_set_qod_type (nvti_t *n, const gchar *qod_type)
{
if (!n)
return (-1);
if (n->qod_type)
g_free (n->qod_type);
if (qod_type && qod_type[0])
n->qod_type = g_strdup (qod_type);
else
n->qod_type = NULL;
return (0);
}
/**
* @brief Set the family of a NVT. * @brief Set the family of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param family The family to set. A copy will be created from this. * @param family The family to set. A copy will be created from this.
* *
* @return 0 for success. Anything else indicates an error. * @return 0 for success. Anything else indicates an error.
*/ */
int int
nvti_set_family (nvti_t *n, const gchar *family) nvti_set_family (nvti_t *n, const gchar *family)
skipping to change at line 827 skipping to change at line 1542
nvti_set_category (nvti_t *n, const gint category) nvti_set_category (nvti_t *n, const gint category)
{ {
if (!n) if (!n)
return (-1); return (-1);
n->category = category; n->category = category;
return (0); return (0);
} }
/** /**
* @brief Add a single CVE ID of a NVT. * @brief Add many new vtref from a comma-separated list.
* *
* @param n The NVT Info structure. * @param n The NVTI where to add the references.
*
* @param type The type for all references. If NULL, then for ref_ids
* a syntax is expected that includes the type like
* "type:id,type:id".
* *
* @param cve_id The CVE ID to add. A copy will be created from this. * @param ref_ids A CSV of reference to be added.
* *
* @return 0 for success. 1 if n was NULL, 2 if cve_id was NULL. * @param ref_text The optional text accompanying all references.
*
* @return 0 for success. 1 if n was NULL, 2 if ref_ids was NULL.
*/ */
int int
nvti_add_cve (nvti_t *n, const gchar *cve_id) nvti_add_refs (nvti_t *n, const gchar *type, const gchar *ref_ids,
const gchar *ref_text)
{ {
gchar *old; gchar **split, **item;
if (!n) if (!n)
return (1); return (1);
if (!cve_id)
if (!ref_ids)
return (2); return (2);
old = n->cve; split = g_strsplit (ref_ids, ",", 0);
if (old) for (item = split; *item; item++)
{ {
n->cve = g_strdup_printf ("%s, %s", old, cve_id); gchar *id;
g_free (old);
}
else
n->cve = g_strdup (cve_id);
return (0); id = *item;
} g_strstrip (id);
/** if (strcmp (id, "") == 0)
* @brief Add a single BID ID of a NVT. continue;
*
* @param n The NVT Info structure.
*
* @param bid_id The BID ID to add. A copy will be created from this.
*
* @return 0 for success. 1 if n was NULL. 2 if bid_id was NULL.
*/
int
nvti_add_bid (nvti_t *n, const gchar *bid_id)
{
gchar *old;
if (!n)
return (1);
if (!bid_id)
return (2);
old = n->bid; if (type)
{
if (old) nvti_add_vtref (n, vtref_new (type, id, ref_text));
{ }
n->bid = g_strdup_printf ("%s, %s", old, bid_id); else
g_free (old); {
gchar **split2;
split2 = g_strsplit (id, ":", 2);
if (split2[0] && split2[1])
nvti_add_vtref (n, vtref_new (split2[0], split2[1], ""));
g_strfreev (split2);
}
} }
else g_strfreev (split);
n->bid = g_strdup (bid_id);
return (0); return (0);
} }
/** /**
* @brief Add a required key of a NVT. * @brief Add a required key of a NVT.
* *
* @param n The NVT Info structure. * @param n The NVT Info structure.
* *
* @param key The required key to add. A copy will be created from this. * @param key The required key to add. A copy will be created from this.
 End of changes. 57 change blocks. 
126 lines changed or deleted 835 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)