Greenbone Vulnerability Management Libraries: util/kb.h File Reference

Greenbone Vulnerability Management Libraries 23.1.2

Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
kb.h File Reference

Knowledge base management API - Redis backend. More...

`#include "../base/nvti.h"`
`#include <assert.h>`
`#include <stddef.h>`
`#include <sys/types.h>`

Go to the source code of this file.

## Data Structures
struct | kb_item
| Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked list. More...
struct | kb
| Top-level KB. This is to be inherited by KB implementations. More...
struct | kb_operations
| KB interface. Functions provided by an implementation. All functions have to be provided, there is no default/fallback. These functions should be called via the corresponding static inline wrappers below. See the wrappers for the documentation. More...

## Macros
#define | KB_PATH_DEFAULT "/run/redis/redis.sock"
| Default KB location.

## Typedefs
typedef struct kb * | kb_t
| type abstraction to hide KB internals.

## Enumerations
enum | kb_item_type { KB_TYPE_UNSPEC , KB_TYPE_INT , KB_TYPE_STR , KB_TYPE_CNT }
| Possible type of a kb_item. More...
enum | kb_nvt_pos { NVT_FILENAME_POS , NVT_REQUIRED_KEYS_POS , NVT_MANDATORY_KEYS_POS , NVT_EXCLUDED_KEYS_POS , NVT_REQUIRED_UDP_PORTS_POS , NVT_REQUIRED_PORTS_POS , NVT_DEPENDENCIES_POS , NVT_TAGS_POS , NVT_CVES_POS , NVT_BIDS_POS , NVT_XREFS_POS , NVT_CATEGORY_POS , NVT_FAMILY_POS , NVT_NAME_POS , NVT_TIMESTAMP_POS , NVT_OID_POS }
| Possible positions of nvt values in cache list. More...

## Functions
void | kb_item_free (struct kb_item *)
| Release a KB item (or a list).
static int | kb_new (kb_t *kb, const char *kb_path)
| Initialize a new Knowledge Base object.
static kb_t | kb_direct_conn (const char *kb_path, const int kb_index)
| Connect to a Knowledge Base object which has the given kb_index.
static kb_t | kb_find (const char *kb_path, const char *key)
| Find an existing Knowledge Base object with key.
static int | kb_delete (kb_t kb)
| Delete all entries and release ownership on the namespace.
static struct kb_item * | kb_item_get_single (kb_t kb, const char *name, enum kb_item_type type)
| Get a single KB element.
static char * | kb_item_get_str (kb_t kb, const char *name)
| Get a single KB string item.
static int | kb_item_get_int (kb_t kb, const char *name)
| Get a single KB integer item.
static struct kb_item * | kb_item_get_all (kb_t kb, const char *name)
| Get all items stored under a given name.
static struct kb_item * | kb_item_get_pattern (kb_t kb, const char *pattern)
| Get all items stored under a given pattern.
static int | kb_item_push_str (kb_t kb, const char *name, const char *value)
| Push a new value under a given key.
static char * | kb_item_pop_str (kb_t kb, const char *name)
| Pop a single KB string item.
static size_t | kb_item_count (kb_t kb, const char *pattern)
| Count all items stored under a given pattern.
static int | kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
| Insert (append) a new entry under a given name.
static int | kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len, int pos)
| Insert (append) a new unique entry under a given name.
static int | kb_add_str_unique_volatile (kb_t kb, const char *name, const char *str, int expire, size_t len, int pos)
| Insert (append) a new unique and volatile entry under a given name.
static int | kb_item_set_str (kb_t kb, const char *name, const char *str, size_t len)
| Set (replace) a new entry under a given name.
static int | kb_item_add_int (kb_t kb, const char *name, int val)
| Insert (append) a new entry under a given name.
static int | kb_item_add_int_unique (kb_t kb, const char *name, int val)
| Insert (append) a new unique entry under a given name.
static int | kb_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire)
| Insert (append) a new unique and volatile entry under a given name.
static int | kb_item_set_int (kb_t kb, const char *name, int val)
| Set (replace) a new entry under a given name.
static int | kb_nvt_add (kb_t kb, const nvti_t *nvt, const char *filename)
| Insert a new nvt.
static char * | kb_nvt_get (kb_t kb, const char *oid, enum kb_nvt_pos position)
| Get field of a NVT.
static nvti_t * | kb_nvt_get_all (kb_t kb, const char *oid)
| Get a full NVT.
static GSList * | kb_nvt_get_oids (kb_t kb)
| Get list of NVT OIDs.
static int | kb_del_items (kb_t kb, const char *name)
| Delete all entries under a given name.
static int | kb_save (kb_t kb)
| Save all the KB's content.
static int | kb_lnk_reset (kb_t kb)
| Reset connection to the KB. This is called after each fork() to make.
static int | kb_flush (kb_t kb, const char *except)
| Flush all the KB's content. Delete all namespaces.
static int | kb_get_kb_index (kb_t kb)
| Return the kb index.

## Variables
const struct kb_operations * | KBDefaultOperations
| Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).

## Detailed Description

Knowledge base management API - Redis backend.

## Macro Definition Documentation

## ◆ KB_PATH_DEFAULT

#define KB_PATH_DEFAULT "/run/redis/redis.sock"

Default KB location.

## Typedef Documentation

## ◆ kb_t

typedef struct kb* kb_t

type abstraction to hide KB internals.

## Enumeration Type Documentation

## ◆ kb_item_type

enum kb_item_type

Possible type of a kb_item.

Enumerator
KB_TYPE_UNSPEC | Ignore the value (name/presence test).
KB_TYPE_INT | The kb_items v should then be interpreted as int.
KB_TYPE_STR | The kb_items v should then be interpreted as char*.
KB_TYPE_CNT |

## ◆ kb_nvt_pos

enum kb_nvt_pos

Possible positions of nvt values in cache list.

Enumerator
NVT_FILENAME_POS |
NVT_REQUIRED_KEYS_POS |
NVT_MANDATORY_KEYS_POS |
NVT_EXCLUDED_KEYS_POS |
NVT_REQUIRED_UDP_PORTS_POS |
NVT_REQUIRED_PORTS_POS |
NVT_DEPENDENCIES_POS |
NVT_TAGS_POS |
NVT_CVES_POS |
NVT_BIDS_POS |
NVT_XREFS_POS |
NVT_CATEGORY_POS |
NVT_FAMILY_POS |
NVT_NAME_POS |
NVT_TIMESTAMP_POS |
NVT_OID_POS |

## Function Documentation

## ◆ kb_add_int_unique_volatile()

int kb_add_int_unique_volatile | ( | kb_t | kb,
|  | const char * | name,
|  | int | val,
|  | int | expire )
| inlinestatic

Insert (append) a new unique and volatile entry under a given name.

Parameters

  [in] | kb | Reference to a kb_t to initialize.
  [in] | name | Item name.
  [in] | val | Item value.
  [in] | expire | Item expire.

Returns
  0 on success, -1 on error.

## ◆ kb_add_str_unique_volatile()

int kb_add_str_unique_volatile | ( | kb_t | kb,
|  | const char * | name,
|  | const char * | str,
|  | int | expire,
|  | size_t | len,
|  | int | pos )
| inlinestatic

Insert (append) a new unique and volatile entry under a given name.

Parameters

  [in] | kb | Reference to a kb_t to initialize.
  [in] | name | Item name.
  [in] | str | String to add.
  [in] | len | Value length. Used for blobs.
  [in] | expire | Item expire.
  [in] | pos | Which position the value is appended to. 0 for right, 1 for left position in the list.

Returns
  0 on success, -1 on error.

## ◆ kb_del_items()

int kb_del_items | ( | kb_t | kb,
|  | const char * | name )
| inlinestatic

Delete all entries under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.

Returns
  0 on success, non-null on error.

## ◆ kb_delete()

int kb_delete | ( | kb_t | kb | ) |
| inlinestatic

Delete all entries and release ownership on the namespace.

Parameters

  [in] | kb | KB handle to release.

Returns
  0 on success, non-null on error.

## ◆ kb_direct_conn()

kb_t kb_direct_conn | ( | const char * | kb_path,
|  | const int | kb_index )
| inlinestatic

Connect to a Knowledge Base object which has the given kb_index.

Parameters

  [in] | kb_path | Path to KB.
  [in] | kb_index | DB index

Returns
  Knowledge Base object, NULL otherwise.

## ◆ kb_find()

kb_t kb_find | ( | const char * | kb_path,
|  | const char * | key )
| inlinestatic

Find an existing Knowledge Base object with key.

Parameters

  [in] | kb_path | Path to KB.
  [in] | key | Marker key to search for in KB objects.

Returns
  Knowledge Base object, NULL otherwise.

## ◆ kb_flush()

int kb_flush | ( | kb_t | kb,
|  | const char * | except )
| inlinestatic

Flush all the KB's content. Delete all namespaces.

Parameters

  [in] | kb | KB handle.
  [in] | except | Don't flush DB with except key.

Returns
  0 on success, non-null on error.

## ◆ kb_get_kb_index()

int kb_get_kb_index | ( | kb_t | kb | ) |
| inlinestatic

Return the kb index.

Parameters

  [in] | kb | KB handle.

Returns
  kb_index on success, null on error.

## ◆ kb_item_add_int()

int kb_item_add_int | ( | kb_t | kb,
|  | const char * | name,
|  | int | val )
| inlinestatic

Insert (append) a new entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | val | Item value.

Returns
  0 on success, non-null on error.

## ◆ kb_item_add_int_unique()

int kb_item_add_int_unique | ( | kb_t | kb,
|  | const char * | name,
|  | int | val )
| inlinestatic

Insert (append) a new unique entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | val | Item value.

Returns
  0 on success, non-null on error.

## ◆ kb_item_add_str()

int kb_item_add_str | ( | kb_t | kb,
|  | const char * | name,
|  | const char * | str,
|  | size_t | len )
| inlinestatic

Insert (append) a new entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | str | Item value.
  [in] | len | Value length. Used for blobs.

Returns
  0 on success, non-null on error.

## ◆ kb_item_add_str_unique()

int kb_item_add_str_unique | ( | kb_t | kb,
|  | const char * | name,
|  | const char * | str,
|  | size_t | len,
|  | int | pos )
| inlinestatic

Insert (append) a new unique entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | str | Item value.
  [in] | len | Value length. Used for blobs.
  [in] | pos | Which position the value is appended to. 0 for right, 1 for left position in the list.

Returns
  0 on success, non-null on error.

## ◆ kb_item_count()

size_t kb_item_count | ( | kb_t | kb,
|  | const char * | pattern )
| inlinestatic

Count all items stored under a given pattern.

Parameters

  [in] | kb | KB handle where to count the items.
  [in] | pattern | '*' pattern of the elements to count.

Returns
  Count of items.

## ◆ kb_item_free()

void kb_item_free | ( | struct kb_item * | item | ) |

Release a KB item (or a list).

Parameters

  [in] | item | Item or list to be release

## ◆ kb_item_get_all()

struct kb_item * kb_item_get_all | ( | kb_t | kb,
|  | const char * | name )
| inlinestatic

Get all items stored under a given name.

Parameters

  [in] | kb | KB handle where to fetch the items.
  [in] | name | Name of the elements to retrieve.

Returns
  Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

## ◆ kb_item_get_int()

int kb_item_get_int | ( | kb_t | kb,
|  | const char * | name )
| inlinestatic

Get a single KB integer item.

Parameters

  [in] | kb | KB handle where to fetch the item.
  [in] | name | Name of the element to retrieve.

Returns
  An integer.

## ◆ kb_item_get_pattern()

struct kb_item * kb_item_get_pattern | ( | kb_t | kb,
|  | const char * | pattern )
| inlinestatic

Get all items stored under a given pattern.

Parameters

  [in] | kb | KB handle where to fetch the items.
  [in] | pattern | '*' pattern of the elements to retrieve.

Returns
  Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

## ◆ kb_item_get_single()

struct kb_item * kb_item_get_single | ( | kb_t | kb,
|  | const char * | name,
|  | enum kb_item_type | type )
| inlinestatic

Get a single KB element.

Parameters

  [in] | kb | KB handle where to fetch the item.
  [in] | name | Name of the element to retrieve.
  [in] | type | Desired element type.

Returns
  A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

## ◆ kb_item_get_str()

char * kb_item_get_str | ( | kb_t | kb,
|  | const char * | name )
| inlinestatic

Get a single KB string item.

Parameters

  [in] | kb | KB handle where to fetch the item.
  [in] | name | Name of the element to retrieve.

Returns
  A string to be freed or NULL if list is empty or on error.

## ◆ kb_item_pop_str()

char * kb_item_pop_str | ( | kb_t | kb,
|  | const char * | name )
| inlinestatic

Pop a single KB string item.

Parameters

  [in] | kb | KB handle where to fetch the item.
  [in] | name | Name of the element to retrieve.

Returns
  A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

## ◆ kb_item_push_str()

int kb_item_push_str | ( | kb_t | kb,
|  | const char * | name,
|  | const char * | value )
| inlinestatic

Push a new value under a given key.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Key to push to.
  [in] | value | Value to push.

Returns
  0 on success, non-null on error.

## ◆ kb_item_set_int()

int kb_item_set_int | ( | kb_t | kb,
|  | const char * | name,
|  | int | val )
| inlinestatic

Set (replace) a new entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | val | Item value.

Returns
  0 on success, non-null on error.

## ◆ kb_item_set_str()

int kb_item_set_str | ( | kb_t | kb,
|  | const char * | name,
|  | const char * | str,
|  | size_t | len )
| inlinestatic

Set (replace) a new entry under a given name.

Parameters

  [in] | kb | KB handle where to store the item.
  [in] | name | Item name.
  [in] | str | Item value.
  [in] | len | Value length. Used for blobs.

Returns
  0 on success, non-null on error.

## ◆ kb_lnk_reset()

int kb_lnk_reset | ( | kb_t | kb | ) |
| inlinestatic

Reset connection to the KB. This is called after each fork() to make.

       sure connections aren't shared between concurrent processes.

Parameters

  [in] | kb | KB handle.

Returns
  0 on success, non-null on error.

## ◆ kb_new()

int kb_new | ( | kb_t * | kb,
|  | const char * | kb_path )
| inlinestatic

Initialize a new Knowledge Base object.

Parameters

  [in] | kb | Reference to a kb_t to initialize.
  [in] | kb_path | Path to KB.

Returns
  0 on success, -1 on connection error, -2 on unavailable DB spot.

## ◆ kb_nvt_add()

int kb_nvt_add | ( | kb_t | kb,
|  | const nvti_t * | nvt,
|  | const char * | filename )
| inlinestatic

Insert a new nvt.

Parameters

  [in] | kb | KB handle where to store the nvt.
  [in] | nvt | nvt to store.
  [in] | filename | Path to nvt to store.

Returns
  0 on success, non-null on error.

## ◆ kb_nvt_get()

char * kb_nvt_get | ( | kb_t | kb,
|  | const char * | oid,
|  | enum kb_nvt_pos | position )
| inlinestatic

Get field of a NVT.

Parameters

  [in] | kb | KB handle where to store the nvt.
  [in] | oid | OID of NVT to get from.
  [in] | position | Position of field to get.

Returns
  Value of field, NULL otherwise.

## ◆ kb_nvt_get_all()

nvti_t * kb_nvt_get_all | ( | kb_t | kb,
|  | const char * | oid )
| inlinestatic

Get a full NVT.

Parameters

  [in] | kb | KB handle where to store the nvt.
  [in] | oid | OID of NVT to get.

Returns
  nvti_t of NVT, NULL otherwise.

## ◆ kb_nvt_get_oids()

GSList * kb_nvt_get_oids | ( | kb_t | kb | ) |
| inlinestatic

Get list of NVT OIDs.

Parameters

  [in] | kb | KB handle where NVTs are stored.

Returns
  Linked-list of OIDs, NULL otherwise.

## ◆ kb_save()

int kb_save | ( | kb_t | kb | ) |
| inlinestatic

Save all the KB's content.

Parameters

  [in] | kb | KB handle.

Returns
  0 on success, non-null on error.

## Variable Documentation

## ◆ KBDefaultOperations

const struct kb_operations* KBDefaultOperations
| extern

Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).