Greenbone Vulnerability Management Libraries 22.32.0
vtparser.h File Reference

Simple JSON reader. More...

#include "../base/cvss.h"
#include "../base/nvti.h"
#include "../util/jsonpull.h"
#include <cjson/cJSON.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for vtparser.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define _GNU_SOURCE   /* See feature_test_macros(7) */
#define _FILE_OFFSET_BITS   64

Enumerations

enum  nvt_category {
  ACT_INIT = 0 , ACT_SCANNER , ACT_SETTINGS , ACT_GATHER_INFO ,
  ACT_ATTACK , ACT_MIXED_ATTACK , ACT_DESTRUCTIVE_ATTACK , ACT_DENIAL ,
  ACT_KILL_HOST , ACT_FLOOD , ACT_END
}
 VT categories. More...

Functions

int parse_vt_json (gvm_json_pull_parser_t *, gvm_json_pull_event_t *, nvti_t **)
 Parse a VT element given in json format.

Detailed Description

Simple JSON reader.

Definition in file vtparser.h.

Macro Definition Documentation

◆ _FILE_OFFSET_BITS

#define _FILE_OFFSET_BITS   64

Definition at line 15 of file vtparser.h.

◆ _GNU_SOURCE

#define _GNU_SOURCE   /* See feature_test_macros(7) */

Definition at line 14 of file vtparser.h.

Enumeration Type Documentation

◆ nvt_category

VT categories.

Enumerator
ACT_INIT 
ACT_SCANNER 
ACT_SETTINGS 
ACT_GATHER_INFO 
ACT_ATTACK 
ACT_MIXED_ATTACK 
ACT_DESTRUCTIVE_ATTACK 
ACT_DENIAL 
ACT_KILL_HOST 
ACT_FLOOD 
ACT_END 

Definition at line 29 of file vtparser.h.

30{
31 ACT_INIT = 0,
41 ACT_END,
nvt_category
VT categories.
Definition vtparser.h:30
@ ACT_KILL_HOST
Definition vtparser.h:39
@ ACT_DESTRUCTIVE_ATTACK
Definition vtparser.h:37
@ ACT_SCANNER
Definition vtparser.h:32
@ ACT_END
Definition vtparser.h:41
@ ACT_FLOOD
Definition vtparser.h:40
@ ACT_GATHER_INFO
Definition vtparser.h:34
@ ACT_DENIAL
Definition vtparser.h:38
@ ACT_ATTACK
Definition vtparser.h:35
@ ACT_SETTINGS
Definition vtparser.h:33
@ ACT_MIXED_ATTACK
Definition vtparser.h:36
@ ACT_INIT
Definition vtparser.h:31

Function Documentation

◆ parse_vt_json()

int parse_vt_json ( gvm_json_pull_parser_t * parser,
gvm_json_pull_event_t * event,
nvti_t ** nvt )

Parse a VT element given in json format.

Parameters
[in]parserJson pull parser.
[in]eventJson pull event.
[out]nvtThe NVT Info structure to fill with the parsed data.
Returns
0 on success, 1 on end of feed, -1 on error. In case of success the nvti struct must be freed with nvti_free() by the caller.

Definition at line 239 of file vtparser.c.

241{
242 cJSON *vt_obj = NULL;
243 gchar *str, *error_message = NULL;
244 *nvt = NULL;
245
246 gvm_json_pull_parser_next (parser, event);
247
248 // Handle start/end of json array
249 gchar *path = gvm_json_path_to_string (event->path);
250 if (!g_strcmp0 (path, "$") && event->type == GVM_JSON_PULL_EVENT_ARRAY_START)
251 {
252 gvm_json_pull_parser_next (parser, event);
253 g_debug ("%s: Start parsing feed", __func__);
254 }
255 else if (!g_strcmp0 (path, "$")
257 || event->type == GVM_JSON_PULL_EVENT_EOF))
258 {
259 g_debug ("%s: Finish parsing feed", __func__);
260 g_free (path);
261 return 1;
262 }
263 g_free (path);
264
265 // It is an NVT object
267 {
268 g_warning ("%s: Error reading VT object", __func__);
269 return -1;
270 }
271
272 vt_obj = gvm_json_pull_expand_container (parser, &error_message);
273 if (!cJSON_IsObject (vt_obj))
274 {
275 g_free (error_message);
276 cJSON_Delete (vt_obj);
277 return -1;
278 }
279 g_free (error_message);
280
281 *nvt = nvti_new ();
282
283 if (gvm_json_obj_check_str (vt_obj, "oid", &str))
284 {
285 g_warning ("%s: VT missing OID", __func__);
286 cJSON_Delete (vt_obj);
287 nvti_free (*nvt);
288 return -1;
289 }
290 nvti_set_oid (*nvt, str);
291
292 if (gvm_json_obj_check_str (vt_obj, "name", &str))
293 {
294 g_warning ("%s: VT missing NAME", __func__);
295 cJSON_Delete (vt_obj);
296 nvti_free (*nvt);
297 return -1;
298 }
299 nvti_set_name (*nvt, str);
300
301 if (gvm_json_obj_check_str (vt_obj, "family", &str))
302 {
303 g_warning ("%s: VT missing FAMILY", __func__);
304 cJSON_Delete (vt_obj);
305 nvti_free (*nvt);
306 return -1;
307 }
308 nvti_set_family (*nvt, str);
309
310 if (gvm_json_obj_check_str (vt_obj, "category", &str))
311 {
312 g_warning ("%s: VT missing CATEGORY", __func__);
313 cJSON_Delete (vt_obj);
314 nvti_free (*nvt);
315 return -1;
316 }
318
319 cJSON *tag_obj = cJSON_GetObjectItem (vt_obj, "tag");
320
321 if (tag_obj)
322 {
323 if (add_tags_to_nvt (*nvt, tag_obj))
324 {
325 g_warning ("%s: Error adding tags", __func__);
326 cJSON_Delete (vt_obj);
327 nvti_free (*nvt);
328 return -1;
329 }
330 }
331
332 parse_references (*nvt, vt_obj);
333 add_preferences_to_nvt (*nvt, vt_obj);
334 cJSON_Delete (vt_obj);
335
336 return 0;
337}
int gvm_json_obj_check_str(cJSON *obj, const gchar *key, gchar **val)
Get a string field from a JSON object.
Definition json.c:142
void gvm_json_pull_parser_next(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Get the next event from a JSON pull parser.
Definition jsonpull.c:669
gchar * gvm_json_path_to_string(GQueue *path)
Converts a path as used by a JSON pull parser to a JSONPath string.
Definition jsonpull.c:910
cJSON * gvm_json_pull_expand_container(gvm_json_pull_parser_t *parser, gchar **error_message)
Expands the current array or object of a JSON pull parser.
Definition jsonpull.c:746
@ GVM_JSON_PULL_EVENT_OBJECT_START
Definition jsonpull.h:45
@ GVM_JSON_PULL_EVENT_EOF
Definition jsonpull.h:51
@ GVM_JSON_PULL_EVENT_ARRAY_END
Definition jsonpull.h:44
@ GVM_JSON_PULL_EVENT_ARRAY_START
Definition jsonpull.h:43
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition nvti.c:559
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition nvti.c:1214
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition nvti.c:1234
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition nvti.c:1903
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition nvti.c:1943
void nvti_free(nvti_t *n)
Free memory of a nvti structure.
Definition nvti.c:570
GQueue * path
Path to the event value.
Definition jsonpull.h:61
gvm_json_pull_event_type_t type
Type of event.
Definition jsonpull.h:60
static void add_preferences_to_nvt(nvti_t *nvt, cJSON *vt_obj)
Definition vtparser.c:185
static int get_category_from_name(const gchar *cat)
Get the VT category type given the category as string.
Definition vtparser.c:27
static void parse_references(nvti_t *nvt, cJSON *vt_obj)
Definition vtparser.c:157
static int add_tags_to_nvt(nvti_t *nvt, cJSON *tag_obj)
Add to the NVT Info structure.
Definition vtparser.c:64

References add_preferences_to_nvt(), add_tags_to_nvt(), get_category_from_name(), gvm_json_obj_check_str(), gvm_json_path_to_string(), GVM_JSON_PULL_EVENT_ARRAY_END, GVM_JSON_PULL_EVENT_ARRAY_START, GVM_JSON_PULL_EVENT_EOF, GVM_JSON_PULL_EVENT_OBJECT_START, gvm_json_pull_expand_container(), gvm_json_pull_parser_next(), nvti_free(), nvti_new(), nvti_set_category(), nvti_set_family(), nvti_set_name(), nvti_set_oid(), parse_references(), gvm_json_pull_event_t::path, and gvm_json_pull_event_t::type.

Referenced by Ensure().

Here is the call graph for this function:
Here is the caller graph for this function: